Skip to content
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
95 changes: 57 additions & 38 deletions __tests__/issueCommentTest/approve.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import nock from 'nock'

import {handleIssueComment} from '../../src/issueComment/handleIssueComment'
import { approve } from '../../src/issueComment/approve'

import * as utils from '../testUtils'

Expand All @@ -15,7 +14,7 @@ describe('/approve', () => {
nock.cleanAll()
utils.setupActionsEnv('/approve')
})
afterEach(()=>{
afterEach(() => {
if (!nock.isDone()) {
throw new Error(
`Not all nock interceptors were used: ${JSON.stringify(
Expand All @@ -24,57 +23,79 @@ describe('/approve', () => {
)
}
})

it('throws if commenter is not an approver in OWNERS', async () => {
const owners = Buffer.from(`

it('fails if commenter is not an approver in OWNERS', async () => {
const owners = Buffer.from(
`
reviewers:
- Codertocat
`).toString('base64')

`
).toString('base64')

const contentResponse = {
type: "file",
encoding: "base64",
type: 'file',
encoding: 'base64',
size: 4096,
name: "OWNERS",
path: "OWNERS",
name: 'OWNERS',
path: 'OWNERS',
content: owners
}

nock(utils.api)
.get('/repos/Codertocat/Hello-World/contents/OWNERS')
.reply(200, contentResponse)

const wantErr = `Codertocat is not included in the approvers role in the OWNERS file`

// Mock the reply that the user is not authorized
nock(utils.api)
.post('/repos/Codertocat/Hello-World/issues/1/comments', (req) => {
expect(req.body).toContain(wantErr)
return true
})
.reply(200)

issueCommentEventAssign.comment.body = '/approve'
const commentContext = new utils.mockContext(issueCommentEventAssign)

expect(() => approve(commentContext))
.rejects.toThrowError('Codertocat is not included in the approvers role in the OWNERS file')
await handleIssueComment(commentContext)
})

it('throws if commenter is not an org member or collaborator', async () => {
it('fails if commenter is not an org member or collaborator', async () => {
const wantErr = `Codertocat is not a org member or collaborator`

// Mock the reply that the user is not authorized
nock(utils.api)
.post('/repos/Codertocat/Hello-World/issues/1/comments', (req) => {
expect(req.body).toContain(wantErr)
return true
})
.reply(200)

nock(utils.api)
.get('/repos/Codertocat/Hello-World/contents/OWNERS')
.reply(404)

issueCommentEventAssign.comment.body = '/approve'
const commentContext = new utils.mockContext(issueCommentEventAssign)

expect(() => approve(commentContext))
.rejects.toThrowError('Codertocat is not a org member or collaborator')
await handleIssueComment(commentContext)
})

it('approves if commenter is an approver in OWNERS', async () => {
const owners = Buffer.from(`
const owners = Buffer.from(
`
approvers:
- Codertocat
`).toString('base64')

`
).toString('base64')

const contentResponse = {
type: "file",
encoding: "base64",
type: 'file',
encoding: 'base64',
size: 4096,
name: "OWNERS",
path: "OWNERS",
name: 'OWNERS',
path: 'OWNERS',
content: owners
}
nock(utils.api)
Expand All @@ -101,9 +122,7 @@ approvers:
.get('/repos/Codertocat/Hello-World/contents/OWNERS')
.reply(404)

nock(utils.api)
.get('/orgs/Codertocat/members/Codertocat')
.reply(204)
nock(utils.api).get('/orgs/Codertocat/members/Codertocat').reply(204)

nock(utils.api)
.get('/repos/Codertocat/Hello-World/collaborators/Codertocat')
Expand All @@ -125,17 +144,19 @@ approvers:
})

it('removes approval with the /approve cancel command if approver in OWNERS file', async () => {
const owners = Buffer.from(`
const owners = Buffer.from(
`
approvers:
- some-user
`).toString('base64')

`
).toString('base64')

const contentResponse = {
type: "file",
encoding: "base64",
type: 'file',
encoding: 'base64',
size: 4096,
name: "OWNERS",
path: "OWNERS",
name: 'OWNERS',
path: 'OWNERS',
content: owners
}
nock(utils.api)
Expand Down Expand Up @@ -169,10 +190,8 @@ approvers:
nock(utils.api)
.get('/repos/Codertocat/Hello-World/contents/OWNERS')
.reply(404)

nock(utils.api)
.get('/orgs/Codertocat/members/some-user')
.reply(404)

nock(utils.api).get('/orgs/Codertocat/members/some-user').reply(404)

nock(utils.api)
.get('/repos/Codertocat/Hello-World/collaborators/some-user')
Expand Down
78 changes: 46 additions & 32 deletions __tests__/label/lgtm.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import nock from 'nock'

import {handleIssueComment} from '../../src/issueComment/handleIssueComment'
import { lgtm } from '../../src/labels/lgtm'

import * as utils from '../testUtils'

Expand All @@ -15,7 +14,7 @@ describe('lgtm', () => {
nock.cleanAll()
utils.setupActionsEnv('/lgtm')
})
afterEach(()=>{
afterEach(() => {
if (!nock.isDone()) {
throw new Error(
`Not all nock interceptors were used: ${JSON.stringify(
Expand All @@ -37,9 +36,7 @@ describe('lgtm', () => {
})
.reply(200)

nock(utils.api)
.get('/orgs/Codertocat/members/Codertocat')
.reply(204)
nock(utils.api).get('/orgs/Codertocat/members/Codertocat').reply(204)

nock(utils.api)
.get('/repos/Codertocat/Hello-World/collaborators/Codertocat')
Expand Down Expand Up @@ -77,9 +74,7 @@ describe('lgtm', () => {
.get('/repos/Codertocat/Hello-World/issues/1')
.reply(200, issuePayload)

nock(utils.api)
.get('/orgs/Codertocat/members/Codertocat')
.reply(204)
nock(utils.api).get('/orgs/Codertocat/members/Codertocat').reply(204)

nock(utils.api)
.get('/repos/Codertocat/Hello-World/collaborators/Codertocat')
Expand All @@ -104,9 +99,7 @@ describe('lgtm', () => {
})
.reply(200)

nock(utils.api)
.get('/orgs/Codertocat/members/Codertocat')
.reply(404)
nock(utils.api).get('/orgs/Codertocat/members/Codertocat').reply(404)

nock(utils.api)
.get('/repos/Codertocat/Hello-World/collaborators/Codertocat')
Expand All @@ -122,44 +115,63 @@ describe('lgtm', () => {
})
})

it('throws if commenter is not reviewer in OWNERS', async () => {
const owners = Buffer.from(`
it('fails if commenter is not reviewer in OWNERS', async () => {
const owners = Buffer.from(
`
approvers:
- Codertocat
`).toString('base64')

`
).toString('base64')

const contentResponse = {
type: "file",
encoding: "base64",
type: 'file',
encoding: 'base64',
size: 4096,
name: "OWNERS",
path: "OWNERS",
name: 'OWNERS',
path: 'OWNERS',
content: owners
}
nock(utils.api)
.get('/repos/Codertocat/Hello-World/contents/OWNERS')
.reply(200, contentResponse)

const wantErr = `Codertocat is not included in the reviewers role in the OWNERS file`

// Mock the reply that the user is not authorized
nock(utils.api)
.post('/repos/Codertocat/Hello-World/issues/1/comments', (req) => {
expect(req.body).toContain(wantErr)
return true
})
.reply(200)

issueCommentEvent.comment.body = '/lgtm'
const commentContext = new utils.mockContext(issueCommentEvent)

expect(() => lgtm(commentContext))
.rejects.toThrowError('Codertocat is not included in the reviewers role in the OWNERS file')
await handleIssueComment(commentContext)
})

it('throws if commenter is not org member or collaborator', async () => {
it('fails if commenter is not org member or collaborator', async () => {
const wantErr = `Codertocat is not a org member or collaborator`

// Mock the reply that the user is not authorized
nock(utils.api)
.post('/repos/Codertocat/Hello-World/issues/1/comments', (req) => {
expect(req.body).toContain(wantErr)
return true
})
.reply(200)
nock(utils.api)
.get('/repos/Codertocat/Hello-World/contents/OWNERS')
.reply(404)

issueCommentEvent.comment.body = '/lgtm'
const commentContext = new utils.mockContext(issueCommentEvent)

expect(() => lgtm(commentContext))
.rejects.toThrowError('Codertocat is not a org member or collaborator')
await handleIssueComment(commentContext)
})

it('adds label if commenter is reviewer in OWNERS', async() => {
it('adds label if commenter is reviewer in OWNERS', async () => {
issueCommentEvent.comment.body = '/lgtm'
const commentContext = new utils.mockContext(issueCommentEvent)

Expand All @@ -171,17 +183,19 @@ approvers:
})
.reply(200)

const owners = Buffer.from(`
const owners = Buffer.from(
`
reviewers:
- Codertocat
`).toString('base64')

`
).toString('base64')

const contentResponse = {
type: "file",
encoding: "base64",
type: 'file',
encoding: 'base64',
size: 4096,
name: "OWNERS",
path: "OWNERS",
name: 'OWNERS',
path: 'OWNERS',
content: owners
}
nock(utils.api)
Expand Down
Loading