-
-
Save tridungle/f3124345d1244097b45128e2cc4fe516 to your computer and use it in GitHub Desktop.
My functional test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { | |
test, trait, beforeEach, afterEach, | |
} = use('Test/Suite')('Admin/Session Github'); | |
const nock = require('nock'); | |
const User = use('App/Models/User'); | |
const responses = require('./utils/responses'); | |
const headers = require('./utils/headers'); | |
trait('Test/ApiClient'); | |
beforeEach(async () => { | |
nock('https://api.github.com') | |
.get('/user') | |
.reply(200, responses.github_user); | |
nock('https://api.github.com') | |
.get('/user/emails') | |
.reply(200, responses.github_emails); | |
nock('https://github.com/login') | |
.get('/oauth/authorize') | |
.query(true) | |
.reply(302, undefined, headers.authorize); | |
nock('http://127.0.0.1:3333') | |
.get('/admin/github/callback') | |
.query(true) | |
.reply(200); | |
nock('https://github.com/login') | |
.post('/oauth/access_token') | |
.reply(responses.access_token.post); | |
nock('https://github.com/login') | |
.get('/oauth/access_token') | |
.query(true) | |
.reply(200, responses.access_token.get); | |
}); | |
afterEach(async () => { | |
await User.query().delete(); | |
}); | |
test('should be redirect to github url', async ({ client }) => { | |
const response = await client.get('/admin/sessions/github').end(); | |
response.assertRedirect('/login/oauth/authorize'); | |
}); | |
test('should be make a new user with github data', async ({ assert, client }) => { | |
const response = await client.get('/admin/github/callback').query({ | |
code: 'fake-code', | |
state: 'fake-uid', | |
}).cookie('oauth_state', 'fake-uid').end(); | |
response.assertStatus(200); | |
response.assertText('Logged in'); | |
const fn = async () => { | |
await User.findByOrFail('email', '[email protected]'); | |
}; | |
assert.doesNotThrow(fn); | |
}); | |
test('should be update user with github data', async ({ assert, client }) => { | |
await User.create({ | |
username: 'rocketseat', | |
name: 'Github Rocketseat', | |
email: '[email protected]', | |
github_id: 1, | |
github_token: 'fake-token', | |
}); | |
const response = await client.get('/admin/github/callback').query({ | |
code: 'fake-code', | |
state: 'fake-uid', | |
}).cookie('oauth_state', 'fake-uid').end(); | |
response.assertStatus(200); | |
response.assertText('Logged in'); | |
const user = (await User.findBy('github_id', 1)).toJSON(); | |
assert.equal(user.email, '[email protected]'); | |
assert.equal(user.name, 'Git Rocketseat'); | |
}); | |
test('should not be make a new user with github data when email already registered', async ({ assert, client }) => { | |
await User.create({ | |
username: 'rocketseat', | |
name: 'Git Rocketseat', | |
email: '[email protected]', | |
}); | |
const response = await client.get('/admin/github/callback').query({ | |
code: 'fake-code', | |
state: 'fake-uid', | |
}).cookie('oauth_state', 'fake-uid').end(); | |
response.assertStatus(200); | |
response.assertText('Unable to authenticate. Try again later'); | |
const user = (await User.findBy('email', '[email protected]')).toJSON(); | |
assert.isNull(user.github_id); | |
assert.isNull(user.github_token); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment