@@ -2,15 +2,21 @@ import * as core from '@actions/core'
22import { validPermissions } from '../../src/functions/valid-permissions'
33
44const setOutputMock = jest . spyOn ( core , 'setOutput' )
5+ const infoMock = jest . spyOn ( core , 'info' )
56
67var octokit
78var context
89beforeEach ( ( ) => {
910 jest . clearAllMocks ( )
1011 jest . spyOn ( core , 'setOutput' ) . mockImplementation ( ( ) => { } )
12+ jest . spyOn ( core , 'info' ) . mockImplementation ( ( ) => { } )
1113 process . env . INPUT_PERMISSIONS = 'write,admin'
1214
1315 context = {
16+ repo : {
17+ owner : 'corp' ,
18+ repo : 'test'
19+ } ,
1420 actor : 'monalisa'
1521 }
1622
@@ -42,6 +48,9 @@ beforeEach(() => {
4248test ( 'determines that a user has valid permissions to invoke the Action' , async ( ) => {
4349 expect ( await validPermissions ( octokit , context ) ) . toEqual ( true )
4450 expect ( setOutputMock ) . toHaveBeenCalledWith ( 'actor' , 'monalisa' )
51+ expect ( infoMock ) . toHaveBeenCalledWith (
52+ `🔍 Detected actor type: User (${ context . actor } )`
53+ )
4554} )
4655
4756test ( 'determines that a user has does not valid permissions to invoke the Action' , async ( ) => {
@@ -60,6 +69,17 @@ test('determines that a user has does not valid permissions to invoke the Action
6069 expect ( setOutputMock ) . toHaveBeenCalledWith ( 'actor' , 'monalisa' )
6170} )
6271
72+ test ( 'fails to get actor information' , async ( ) => {
73+ octokit . rest . users . getByUsername = jest . fn ( ) . mockReturnValue ( {
74+ status : 500
75+ } )
76+
77+ expect ( await validPermissions ( octokit , context ) ) . toEqual (
78+ 'Fetch user details returns non-200 status: 500'
79+ )
80+ expect ( setOutputMock ) . toHaveBeenCalledWith ( 'actor' , 'monalisa' )
81+ } )
82+
6383test ( 'fails to get actor permissions due to a bad status code' , async ( ) => {
6484 octokit . rest . repos . getCollaboratorPermissionLevel = jest
6585 . fn ( )
@@ -76,14 +96,14 @@ test('fails to get actor permissions due to a bad status code', async () => {
7696test ( 'determines that a GitHub App has valid permissions' , async ( ) => {
7797 context . actor = 'github-actions[bot]'
7898
79- octokit . rest . users . getByUsername . mockReturnValueOnce ( {
99+ octokit . rest . users . getByUsername = jest . fn ( ) . mockReturnValueOnce ( {
80100 status : 200 ,
81101 data : {
82102 type : 'Bot'
83103 }
84104 } )
85105
86- octokit . rest . apps . getRepoInstallation . mockReturnValueOnce ( {
106+ octokit . rest . apps . getRepoInstallation = jest . fn ( ) . mockReturnValueOnce ( {
87107 status : 200 ,
88108 data : {
89109 permissions : {
@@ -94,19 +114,22 @@ test('determines that a GitHub App has valid permissions', async () => {
94114
95115 expect ( await validPermissions ( octokit , context ) ) . toEqual ( true )
96116 expect ( setOutputMock ) . toHaveBeenCalledWith ( 'actor' , 'github-actions[bot]' )
117+ expect ( infoMock ) . toHaveBeenCalledWith (
118+ `🔍 Detected actor type: Bot (${ context . actor } )`
119+ )
97120} )
98121
99122test ( 'determines that a GitHub App does not have valid permissions' , async ( ) => {
100123 context . actor = 'monalisa[bot]'
101124
102- octokit . rest . users . getByUsername . mockReturnValueOnce ( {
125+ octokit . rest . users . getByUsername = jest . fn ( ) . mockReturnValueOnce ( {
103126 status : 200 ,
104127 data : {
105128 type : 'Bot'
106129 }
107130 } )
108131
109- octokit . rest . apps . getRepoInstallation . mockReturnValueOnce ( {
132+ octokit . rest . apps . getRepoInstallation = jest . fn ( ) . mockReturnValueOnce ( {
110133 status : 200 ,
111134 data : {
112135 permissions : {
@@ -124,14 +147,14 @@ test('determines that a GitHub App does not have valid permissions', async () =>
124147test ( 'fails to fetch installation details for GitHub App' , async ( ) => {
125148 context . actor = 'monalisa[bot]'
126149
127- octokit . rest . users . getByUsername . mockReturnValueOnce ( {
150+ octokit . rest . users . getByUsername = jest . fn ( ) . mockReturnValueOnce ( {
128151 status : 200 ,
129152 data : {
130153 type : 'Bot'
131154 }
132155 } )
133156
134- octokit . rest . apps . getRepoInstallation . mockReturnValueOnce ( {
157+ octokit . rest . apps . getRepoInstallation = jest . fn ( ) . mockReturnValueOnce ( {
135158 status : 500
136159 } )
137160
0 commit comments