Skip to content

Instantly share code, notes, and snippets.

@HigoRibeiro
Created May 1, 2019 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save HigoRibeiro/27f2fe9dc5d5d44f8a25e6b043b72ac2 to your computer and use it in GitHub Desktop.
Save HigoRibeiro/27f2fe9dc5d5d44f8a25e6b043b72ac2 to your computer and use it in GitHub Desktop.
My functional test
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', 'git@rocketseat.com.br');
};
assert.doesNotThrow(fn);
});
test('should be update user with github data', async ({ assert, client }) => {
await User.create({
username: 'rocketseat',
name: 'Github Rocketseat',
email: 'github@rocketseat.com.br',
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, 'git@rocketseat.com.br');
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: 'git@rocketseat.com.br',
});
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', 'git@rocketseat.com.br')).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