Created
March 29, 2020 22:14
-
-
Save nemrosim/6cb1b75a01f2b6141734298b0cbbfa67 to your computer and use it in GitHub Desktop.
Test for creating new user route
This file contains 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 } = use('Test/Suite')('User'); | |
const User = use('App/Models/User'); | |
trait('Test/ApiClient'); | |
trait('Auth/Client'); | |
trait('DatabaseTransactions'); | |
test('create new user', async ({ assert, client }) => { | |
const EMAIL = 'test@gmail.com'; | |
const PASSWORD = '123'; | |
const user = await User.create({ | |
username: 'some', | |
email: EMAIL, | |
password: PASSWORD, | |
}); | |
assert.equal(user.email, EMAIL); | |
assert.equal(typeof user.password === 'string', true); | |
const login = await client | |
.post('api/v1/login') | |
.send({ | |
email: EMAIL, | |
password: PASSWORD, | |
}) | |
.end(); // You must call "end" to execute HTTP client requests. | |
login.assertStatus(200); | |
const loginResponseJson = JSON.parse(login.text); | |
assert.equal(loginResponseJson.type, 'bearer'); | |
assert.equal(Object.keys(loginResponseJson).length, 3); | |
assert.equal(loginResponseJson.token.length, 141); | |
assert.equal(loginResponseJson.refreshToken.length, 96); | |
const response = await client | |
.post('api/v1/users') | |
.header('accept', 'application/json') | |
.header('Authorization', `Bearer ${loginResponseJson.token}`) | |
.send({ | |
username: 'someName', | |
email: 'hello@gmail.com', | |
password: '123', | |
}) | |
// You must call "end" to execute HTTP client requests. | |
.end(); | |
response.assertStatus(204); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment