Created
November 23, 2019 11:53
-
-
Save henriqueweiand/f04cc5f16507e7eab21d5f1faf76e7c2 to your computer and use it in GitHub Desktop.
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 } = use('Test/Suite')('User registration'); | |
const Factory = use('Factory'); | |
const Database = use('Database'); | |
const User = use('App/Models/User'); | |
const Permissions = use('App/Models/Permissions'); | |
const UserService = use('App/Services/UserService'); | |
const userService = new UserService(); | |
trait('DatabaseTransactions'); | |
trait('Test/ApiClient'); | |
trait('Auth/Client'); | |
let adminUser = false; | |
beforeEach(async () => { | |
// await Database.raw('SET FOREIGN_KEY_CHECKS = 0;'); | |
await User.query().delete(); | |
await User.query().truncate(); | |
await Permissions.query().delete(); | |
await Permissions.query().truncate(); | |
// await Database.raw('SET FOREIGN_KEY_CHECKS = 1;'); | |
const permissions = await Factory.model('App/Models/Permissions').createMany( | |
2, | |
[{ slug: 'administrator' }, { slug: 'client' }] | |
); | |
adminUser = await userService.store({ | |
name: 'henrique', | |
username: 'henrique', | |
email: '[email protected]', | |
password: 'test,.123', | |
permissions: [ | |
permissions[0].id, // 2 - admin permission | |
], | |
}); | |
}); | |
test('Cria um usuário', async ({ client }) => { | |
const number = await Math.floor(Math.random() * 100); | |
const userData = { | |
name: 'novo user', | |
username: 'novouser', | |
email: `novouser-${number}@gmail.com`, | |
}; | |
const response = await client | |
.post('/users') | |
.send({ | |
...userData, | |
password: 'test,.123', | |
password_confirmation: 'test,.123', | |
}) | |
.end(); | |
response.assertStatus(201); | |
response.assertJSONSubset(userData); | |
}).timeout(6000); | |
test('Atualiza um usuário', async ({ client }) => { | |
const user = await Factory.model('App/Models/User').create(); | |
const response = await client | |
.put(`/users/${user.id}`) | |
.loginVia(adminUser) | |
.send({ | |
username: 'nomeatualizado', | |
email: user.email, | |
}) | |
.end(); | |
response.assertStatus(200); | |
response.assertJSONSubset({ | |
username: 'nomeatualizado', | |
}); | |
}).timeout(6000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment