Skip to content

Commit 6f1e161

Browse files
committed
feat: add check githubOrgMFA
Related: #43
1 parent ccd41d3 commit 6f1e161

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

__tests__/checks/githubOrgMFA.test.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
const knexInit = require('knex')
2+
const { getConfig } = require('../../src/config')
3+
const githubOrgMFA = require('../../src/checks/complianceChecks/githubOrgMFA')
4+
const {
5+
resetDatabase, addProject, addGithubOrg, getAllResults, getAllTasks, getAllAlerts,
6+
addAlert, addTask, addResult, getCheckByCodeName
7+
} = require('../../__utils__')
8+
const { sampleGithubOrg } = require('../../__fixtures__')
9+
10+
const { dbSettings } = getConfig('test')
11+
12+
let knex
13+
let project
14+
let check
15+
16+
beforeAll(async () => {
17+
knex = knexInit(dbSettings)
18+
check = await getCheckByCodeName(knex, 'githubOrgMFA')
19+
})
20+
21+
beforeEach(async () => {
22+
await resetDatabase(knex)
23+
project = await addProject(knex, { name: sampleGithubOrg.login, category: 'impact' })
24+
})
25+
26+
afterAll(async () => {
27+
await knex.destroy()
28+
})
29+
30+
describe('Integration: githubOrgMFA', () => {
31+
test('Should add results without alerts or tasks', async () => {
32+
// Add a passed check scenario
33+
await addGithubOrg(knex, { login: sampleGithubOrg.login, html_url: sampleGithubOrg.html_url, project_id: project.id, two_factor_requirement_enabled: true })
34+
// Check that the database is empty
35+
let results = await getAllResults(knex)
36+
expect(results.length).toBe(0)
37+
let alerts = await getAllAlerts(knex)
38+
expect(alerts.length).toBe(0)
39+
let tasks = await getAllTasks(knex)
40+
expect(tasks.length).toBe(0)
41+
// Run the check
42+
await expect(githubOrgMFA(knex)).resolves.toBeUndefined()
43+
// Check that the database has the expected results
44+
results = await getAllResults(knex)
45+
expect(results.length).toBe(1)
46+
expect(results[0].status).toBe('passed')
47+
expect(results[0].compliance_check_id).toBe(check.id)
48+
alerts = await getAllAlerts(knex)
49+
expect(alerts.length).toBe(0)
50+
tasks = await getAllTasks(knex)
51+
expect(tasks.length).toBe(0)
52+
})
53+
54+
test('Should delete (previous alerts and tasks) and add results', async () => {
55+
// Prepare the Scenario
56+
await addGithubOrg(knex, { login: sampleGithubOrg.login, html_url: sampleGithubOrg.html_url, project_id: project.id, two_factor_requirement_enabled: true })
57+
await addAlert(knex, { compliance_check_id: check.id, project_id: project.id, title: 'existing', description: 'existing', severity: 'critical' })
58+
await addTask(knex, { compliance_check_id: check.id, project_id: project.id, title: 'existing', description: 'existing', severity: 'critical' })
59+
// Check that the database has the expected results
60+
let results = await getAllResults(knex)
61+
expect(results.length).toBe(0)
62+
let alerts = await getAllAlerts(knex)
63+
expect(alerts.length).toBe(1)
64+
expect(alerts[0].compliance_check_id).toBe(check.id)
65+
let tasks = await getAllTasks(knex)
66+
expect(tasks.length).toBe(1)
67+
expect(tasks[0].compliance_check_id).toBe(check.id)
68+
// Run the check
69+
await githubOrgMFA(knex)
70+
// Check that the database has the expected results
71+
results = await getAllResults(knex)
72+
expect(results.length).toBe(1)
73+
expect(results[0].status).toBe('passed')
74+
alerts = await getAllAlerts(knex)
75+
expect(alerts.length).toBe(0)
76+
tasks = await getAllTasks(knex)
77+
expect(tasks.length).toBe(0)
78+
})
79+
test('Should add (alerts and tasks) and update results', async () => {
80+
// Prepare the Scenario
81+
await addGithubOrg(knex, { login: sampleGithubOrg.login, html_url: sampleGithubOrg.html_url, project_id: project.id, two_factor_requirement_enabled: false })
82+
await addResult(knex, { compliance_check_id: check.id, project_id: project.id, status: 'passed', rationale: 'failed previously', severity: 'critical' })
83+
// Check that the database has the expected results
84+
let results = await getAllResults(knex)
85+
expect(results.length).toBe(1)
86+
expect(results[0].compliance_check_id).toBe(check.id)
87+
let alerts = await getAllAlerts(knex)
88+
expect(alerts.length).toBe(0)
89+
let tasks = await getAllTasks(knex)
90+
expect(tasks.length).toBe(0)
91+
// Run the check
92+
await githubOrgMFA(knex)
93+
// Check that the database has the expected results
94+
results = await getAllResults(knex)
95+
expect(results.length).toBe(1)
96+
expect(results[0].status).toBe('failed')
97+
expect(results[0].rationale).not.toBe('failed previously')
98+
alerts = await getAllAlerts(knex)
99+
expect(alerts.length).toBe(1)
100+
expect(alerts[0].compliance_check_id).toBe(check.id)
101+
tasks = await getAllTasks(knex)
102+
expect(tasks.length).toBe(1)
103+
expect(tasks[0].compliance_check_id).toBe(check.id)
104+
})
105+
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const validators = require('../validators')
2+
const { initializeStore } = require('../../store')
3+
const debug = require('debug')('checks:githubOrgMFA')
4+
5+
module.exports = async (knex) => {
6+
const {
7+
getAllGithubOrganizations, getCheckByCodeName,
8+
getAllProjects, addAlert, addTask, upsertComplianceCheckResult,
9+
deleteAlertsByComplianceCheckId, deleteTasksByComplianceCheckId
10+
} = initializeStore(knex)
11+
debug('Collecting relevant data...')
12+
const check = await getCheckByCodeName('githubOrgMFA')
13+
const organizations = await getAllGithubOrganizations()
14+
const projects = await getAllProjects()
15+
16+
debug('Extracting the validation results...')
17+
const analysis = validators.githubOrgMFA({ organizations, check, projects })
18+
19+
debug('Deleting previous alerts and tasks to avoid orphaned records...')
20+
await deleteAlertsByComplianceCheckId(check.id)
21+
await deleteTasksByComplianceCheckId(check.id)
22+
23+
debug('Upserting the new results...')
24+
await Promise.all(analysis.results.map(result => upsertComplianceCheckResult(result)))
25+
26+
debug('Inserting the new Alerts and Tasks...')
27+
await Promise.all(analysis.alerts.map(alert => addAlert(alert)))
28+
await Promise.all(analysis.tasks.map(task => addTask(task)))
29+
}

0 commit comments

Comments
 (0)