Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed Context #4

Merged
merged 5 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 61 additions & 3 deletions __tests__/functions/context-check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ const saveStateMock = jest.spyOn(core, 'saveState')
var context
beforeEach(() => {
jest.clearAllMocks()
process.env.INPUT_ALLOWED_CONTEXTS = 'pull_request,issue'
jest.spyOn(core, 'warning').mockImplementation(() => {})
jest.spyOn(core, 'saveState').mockImplementation(() => {})
jest.spyOn(core, 'debug').mockImplementation(() => {})

context = {
eventName: 'issue_comment',
Expand All @@ -24,14 +26,70 @@ beforeEach(() => {
})

test('checks the event context and finds that it is valid', async () => {
expect(await contextCheck(context)).toBe(true)
expect(await contextCheck(context)).toStrictEqual({
valid: true,
context: 'pull_request'
})
})

test('checks the event context for an issue comment and finds that it is valid', async () => {
context.payload.issue = {}
expect(await contextCheck(context)).toStrictEqual({
valid: true,
context: 'issue'
})
})

test('checks the event context for an issue comment and finds that it is valid - when only issue comments are allowed', async () => {
process.env.INPUT_ALLOWED_CONTEXTS = 'issue'
context.payload.issue = {}
expect(await contextCheck(context)).toStrictEqual({
valid: true,
context: 'issue'
})
})

test('checks the event context for a pr comment and finds that it is valid - when only pr comments are allowed', async () => {
process.env.INPUT_ALLOWED_CONTEXTS = 'pull_request'
expect(await contextCheck(context)).toStrictEqual({
valid: true,
context: 'pull_request'
})
})

test('checks the event context for a pr comment and finds that it is invalid - when only pr comments are allowed', async () => {
process.env.INPUT_ALLOWED_CONTEXTS = 'pull_request'
context.payload.issue = {}
expect(await contextCheck(context)).toStrictEqual({
valid: false,
context: 'issue_comment'
})

expect(warningMock).toHaveBeenCalledWith(
'this Action can only be run in the context of a pull request comment'
)
})

test('checks the event context for an issue comment and finds that it is invalid - when only issue comments are allowed', async () => {
process.env.INPUT_ALLOWED_CONTEXTS = 'issue'
expect(await contextCheck(context)).toStrictEqual({
valid: false,
context: 'issue_comment'
})

expect(warningMock).toHaveBeenCalledWith(
'this Action can only be run in the context of an issue comment'
)
})

test('checks the event context and finds that it is invalid', async () => {
context.eventName = 'push'
expect(await contextCheck(context)).toBe(false)
expect(await contextCheck(context)).toStrictEqual({
valid: false,
context: 'push'
})
expect(warningMock).toHaveBeenCalledWith(
'This Action can only be run in the context of a pull request comment'
'this Action can only be run in the context of an issue_comment'
)
expect(saveStateMock).toHaveBeenCalledWith('bypass', 'true')
})
Expand Down
4 changes: 2 additions & 2 deletions __tests__/functions/post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ beforeEach(() => {
return validStates[name]
})
jest.spyOn(contextCheck, 'contextCheck').mockImplementation(() => {
return true
return {valid: true, context: 'pull_request'}
})

jest.spyOn(postReactions, 'postReactions').mockImplementation(() => {
Expand All @@ -59,7 +59,7 @@ test('successfully runs post() Action logic when "success" is false', async () =

test('exits due to an invalid Actions context', async () => {
jest.spyOn(contextCheck, 'contextCheck').mockImplementation(() => {
return false
return {valid: false, context: 'pull_request'}
})
expect(await post()).toBeUndefined()
})
Expand Down
Loading