|
| 1 | +const { githubOrgMFA } = require('../../src/checks/validators') |
| 2 | +// @see: https://github.com/secure-dashboards/openjs-foundation-dashboard/issues/43 |
| 3 | +describe('githubOrgMFA', () => { |
| 4 | + let organizations, check, projects |
| 5 | + beforeEach(() => { |
| 6 | + organizations = [ |
| 7 | + { |
| 8 | + project_id: 1, |
| 9 | + login: 'org1', |
| 10 | + two_factor_requirement_enabled: true |
| 11 | + }, |
| 12 | + { |
| 13 | + project_id: 1, |
| 14 | + login: 'org2', |
| 15 | + two_factor_requirement_enabled: true |
| 16 | + }, |
| 17 | + { |
| 18 | + project_id: 2, |
| 19 | + login: 'org3', |
| 20 | + two_factor_requirement_enabled: true |
| 21 | + } |
| 22 | + ] |
| 23 | + |
| 24 | + check = { |
| 25 | + id: 1, |
| 26 | + priority_group: 'P1', |
| 27 | + details_url: 'https://example.com', |
| 28 | + level_incubating_status: 'expected', |
| 29 | + level_active_status: 'expected', |
| 30 | + level_retiring_status: 'expected' |
| 31 | + } |
| 32 | + |
| 33 | + projects = [ |
| 34 | + { |
| 35 | + id: 1, |
| 36 | + category: 'impact' |
| 37 | + }, |
| 38 | + { |
| 39 | + id: 2, |
| 40 | + category: 'at-large' |
| 41 | + } |
| 42 | + ] |
| 43 | + }) |
| 44 | + it('Should generate a passed result if all organizations have 2FA enabled', () => { |
| 45 | + const analysis = githubOrgMFA({ organizations, check, projects }) |
| 46 | + expect(analysis).toEqual({ |
| 47 | + alerts: [], |
| 48 | + results: [ |
| 49 | + { |
| 50 | + project_id: 1, |
| 51 | + compliance_check_id: 1, |
| 52 | + severity: 'critical', |
| 53 | + status: 'passed', |
| 54 | + rationale: 'The organization(s) have 2FA enabled' |
| 55 | + }, |
| 56 | + { |
| 57 | + compliance_check_id: 1, |
| 58 | + project_id: 2, |
| 59 | + rationale: 'The organization(s) have 2FA enabled', |
| 60 | + severity: 'critical', |
| 61 | + status: 'passed' |
| 62 | + } |
| 63 | + ], |
| 64 | + tasks: [] |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + it('should generate a failed result if some organizations do not have 2FA enabled', () => { |
| 69 | + organizations[0].two_factor_requirement_enabled = false |
| 70 | + // IMPORTANT: If one organization fails, the whole project fails no matter how other organizations are in the project |
| 71 | + organizations[1].two_factor_requirement_enabled = null |
| 72 | + |
| 73 | + const analysis = githubOrgMFA({ organizations, check, projects }) |
| 74 | + expect(analysis).toEqual({ |
| 75 | + alerts: [ |
| 76 | + { |
| 77 | + project_id: 1, |
| 78 | + compliance_check_id: 1, |
| 79 | + severity: 'critical', |
| 80 | + title: 'The organization(s) (org1) do not have 2FA enabled', |
| 81 | + description: 'Check the details on https://example.com' |
| 82 | + } |
| 83 | + ], |
| 84 | + results: [ |
| 85 | + { |
| 86 | + project_id: 1, |
| 87 | + compliance_check_id: 1, |
| 88 | + severity: 'critical', |
| 89 | + status: 'failed', |
| 90 | + rationale: 'The organization(s) (org1) do not have 2FA enabled' |
| 91 | + }, |
| 92 | + { |
| 93 | + project_id: 2, |
| 94 | + compliance_check_id: 1, |
| 95 | + severity: 'critical', |
| 96 | + status: 'passed', |
| 97 | + rationale: 'The organization(s) have 2FA enabled' |
| 98 | + } |
| 99 | + ], |
| 100 | + tasks: [ |
| 101 | + { |
| 102 | + project_id: 1, |
| 103 | + compliance_check_id: 1, |
| 104 | + severity: 'critical', |
| 105 | + title: 'Enable 2FA for the organization(s) (org1)', |
| 106 | + description: 'Check the details on https://example.com' |
| 107 | + } |
| 108 | + ] |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + it('should generate an unknown result if some organizations have unknown 2FA status', () => { |
| 113 | + organizations[1].two_factor_requirement_enabled = null |
| 114 | + const analysis = githubOrgMFA({ organizations, check, projects }) |
| 115 | + expect(analysis).toEqual({ |
| 116 | + alerts: [], |
| 117 | + results: [ |
| 118 | + { |
| 119 | + project_id: 1, |
| 120 | + compliance_check_id: 1, |
| 121 | + severity: 'critical', |
| 122 | + status: 'unknown', |
| 123 | + rationale: 'The organization(s) (org2) have 2FA status unknown' |
| 124 | + }, |
| 125 | + { |
| 126 | + project_id: 2, |
| 127 | + compliance_check_id: 1, |
| 128 | + severity: 'critical', |
| 129 | + status: 'passed', |
| 130 | + rationale: 'The organization(s) have 2FA enabled' |
| 131 | + } |
| 132 | + ], |
| 133 | + tasks: [] |
| 134 | + }) |
| 135 | + }) |
| 136 | + |
| 137 | + it('Should skip the check if it is not in scope for the project category', () => { |
| 138 | + check.level_active_status = 'n/a' |
| 139 | + check.level_incubating_status = 'n/a' |
| 140 | + check.level_retiring_status = 'n/a' |
| 141 | + const analysis = githubOrgMFA({ organizations, check, projects }) |
| 142 | + expect(analysis).toEqual({ |
| 143 | + alerts: [], |
| 144 | + results: [], |
| 145 | + tasks: [] |
| 146 | + }) |
| 147 | + }) |
| 148 | +}) |
0 commit comments