11const Author = require ( '../../../lib/filters/author' )
22const Helper = require ( '../../../__fixtures__/unit/helper' )
3+ const Teams = require ( '../../../lib/validators/options_processor/teams' )
4+
5+ const authorName = 'mergeabletestauthorname'
6+ const otherAuthorName = 'someone-else'
37
48test ( 'should fail with unexpected author' , async ( ) => {
59 const author = new Author ( )
610 const settings = {
711 do : 'author' ,
812 must_include : {
9- regex : 'someone-else'
13+ regex : otherAuthorName
1014 }
1115 }
12- const filter = await author . processFilter ( createMockContext ( 'mergeable' ) , settings )
16+ const filter = await author . processFilter ( createMockContext ( authorName ) , settings )
1317 expect ( filter . status ) . toBe ( 'fail' )
1418} )
1519
@@ -18,10 +22,10 @@ test('should pass with expected author', async () => {
1822 const settings = {
1923 do : 'author' ,
2024 must_include : {
21- regex : 'mergeable'
25+ regex : authorName
2226 }
2327 }
24- const filter = await author . processFilter ( createMockContext ( 'mergeable' ) , settings )
28+ const filter = await author . processFilter ( createMockContext ( authorName ) , settings )
2529 expect ( filter . status ) . toBe ( 'pass' )
2630} )
2731
@@ -30,10 +34,10 @@ test('should fail with excluded author', async () => {
3034 const settings = {
3135 do : 'author' ,
3236 must_exclude : {
33- regex : 'mergeable'
37+ regex : authorName
3438 }
3539 }
36- const filter = await author . processFilter ( createMockContext ( 'mergeable' ) , settings )
40+ const filter = await author . processFilter ( createMockContext ( authorName ) , settings )
3741 expect ( filter . status ) . toBe ( 'fail' )
3842} )
3943
@@ -42,13 +46,78 @@ test('should pass with excluded author', async () => {
4246 const settings = {
4347 do : 'author' ,
4448 must_exclude : {
45- regex : 'someone-else'
49+ regex : otherAuthorName
4650 }
4751 }
48- const filter = await author . processFilter ( createMockContext ( 'mergeable' ) , settings )
52+ const filter = await author . processFilter ( createMockContext ( authorName ) , settings )
53+ expect ( filter . 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 filter = await author . processFilter ( createMockContext ( authorName ) , settings )
4967 expect ( filter . status ) . toBe ( 'pass' )
5068} )
5169
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 filter = await author . processFilter ( createMockContext ( authorName ) , settings )
81+ expect ( filter . 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 filter = await author . processFilter ( createMockContext ( authorName ) , settings )
95+ expect ( filter . 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 filter = await author . processFilter ( createMockContext ( authorName ) , settings )
106+ expect ( filter . 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 filter = await author . processFilter ( createMockContext ( authorName ) , settings )
118+ expect ( filter . status ) . toBe ( 'fail' )
119+ } )
120+
52121const createMockContext = ( author ) => {
53122 return Helper . mockContext ( { author } )
54123}
0 commit comments