|
| 1 | +const Author = require('../../../lib/validators/author') |
| 2 | +const Helper = require('../../../__fixtures__/unit/helper') |
| 3 | +const Teams = require('../../../lib/validators/options_processor/teams') |
| 4 | + |
| 5 | +const authorName = 'mergeabletestauthorname' |
| 6 | +const otherAuthorName = 'someone-else' |
| 7 | + |
| 8 | +test('should fail with unexpected author', async () => { |
| 9 | + const author = new Author() |
| 10 | + const settings = { |
| 11 | + do: 'author', |
| 12 | + must_include: { |
| 13 | + regex: otherAuthorName |
| 14 | + } |
| 15 | + } |
| 16 | + const validation = await author.processValidate(createMockContext(authorName), settings) |
| 17 | + expect(validation.status).toBe('fail') |
| 18 | +}) |
| 19 | + |
| 20 | +test('should pass with expected author', async () => { |
| 21 | + const author = new Author() |
| 22 | + const settings = { |
| 23 | + do: 'author', |
| 24 | + must_include: { |
| 25 | + regex: authorName |
| 26 | + } |
| 27 | + } |
| 28 | + const validation = await author.processValidate(createMockContext(authorName), settings) |
| 29 | + expect(validation.status).toBe('pass') |
| 30 | +}) |
| 31 | + |
| 32 | +test('should fail with excluded author', async () => { |
| 33 | + const author = new Author() |
| 34 | + const settings = { |
| 35 | + do: 'author', |
| 36 | + must_exclude: { |
| 37 | + regex: authorName |
| 38 | + } |
| 39 | + } |
| 40 | + const validation = await author.processValidate(createMockContext(authorName), settings) |
| 41 | + expect(validation.status).toBe('fail') |
| 42 | +}) |
| 43 | + |
| 44 | +test('should pass with excluded author', async () => { |
| 45 | + const author = new Author() |
| 46 | + const settings = { |
| 47 | + do: 'author', |
| 48 | + must_exclude: { |
| 49 | + regex: otherAuthorName |
| 50 | + } |
| 51 | + } |
| 52 | + const validation = await author.processValidate(createMockContext(authorName), settings) |
| 53 | + expect(validation.status).toBe('pass') |
| 54 | +}) |
| 55 | + |
| 56 | +test('should pass with expected author from correct team', async () => { |
| 57 | + const author = new Author() |
| 58 | + const settings = { |
| 59 | + do: 'author', |
| 60 | + must_include: { |
| 61 | + regex: authorName |
| 62 | + }, |
| 63 | + team: 'org/team-slug' |
| 64 | + } |
| 65 | + Teams.extractTeamMemberships = jest.fn().mockReturnValue([authorName]) |
| 66 | + const validation = await author.processValidate(createMockContext(authorName), settings) |
| 67 | + expect(validation.status).toBe('pass') |
| 68 | +}) |
| 69 | + |
| 70 | +test('should fail with expected author from incorrect team', async () => { |
| 71 | + const author = new Author() |
| 72 | + const settings = { |
| 73 | + do: 'author', |
| 74 | + must_include: { |
| 75 | + regex: authorName |
| 76 | + }, |
| 77 | + team: 'org/team-slug' |
| 78 | + } |
| 79 | + Teams.extractTeamMemberships = jest.fn().mockReturnValue([]) |
| 80 | + const validation = await author.processValidate(createMockContext(authorName), settings) |
| 81 | + expect(validation.status).toBe('fail') |
| 82 | +}) |
| 83 | + |
| 84 | +test('should fail with unexpected author from correct team', async () => { |
| 85 | + const author = new Author() |
| 86 | + const settings = { |
| 87 | + do: 'author', |
| 88 | + must_include: { |
| 89 | + regex: otherAuthorName |
| 90 | + }, |
| 91 | + team: 'org/team-slug' |
| 92 | + } |
| 93 | + Teams.extractTeamMemberships = jest.fn().mockReturnValue([authorName]) |
| 94 | + const validation = await author.processValidate(createMockContext(authorName), settings) |
| 95 | + expect(validation.status).toBe('fail') |
| 96 | +}) |
| 97 | + |
| 98 | +test('should pass when the author is a member of the team', async () => { |
| 99 | + const author = new Author() |
| 100 | + const settings = { |
| 101 | + do: 'author', |
| 102 | + team: 'org/team-slug' |
| 103 | + } |
| 104 | + Teams.extractTeamMemberships = jest.fn().mockReturnValue([authorName]) |
| 105 | + const validation = await author.processValidate(createMockContext(authorName), settings) |
| 106 | + expect(validation.status).toBe('pass') |
| 107 | +}) |
| 108 | + |
| 109 | +test('should fail when the author is not a member of the team', async () => { |
| 110 | + const author = new Author() |
| 111 | + const authorName = 'mergeable' |
| 112 | + const settings = { |
| 113 | + do: 'author', |
| 114 | + team: 'org/team-slug' |
| 115 | + } |
| 116 | + Teams.extractTeamMemberships = jest.fn().mockReturnValue([otherAuthorName]) |
| 117 | + const validation = await author.processValidate(createMockContext(authorName), settings) |
| 118 | + expect(validation.status).toBe('fail') |
| 119 | +}) |
| 120 | + |
| 121 | +const createMockContext = (author) => { |
| 122 | + return Helper.mockContext({ author }) |
| 123 | +} |
0 commit comments