Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const { test, trait, before, after } = use('Test/Suite')('Api User Signup');
- const API_URL = '/api/v1/users';
- const userModel = use('App/Models/User');
- trait('Test/ApiClient');
- // executed before all the tests for a given suite
- before(async () => {
- await userModel.query().where('username', 'unittest').delete();
- });
- // executed after all the tests for a given suite
- after(async () => {
- await userModel.query().where('username', 'unittest').delete();
- });
- test('Should throw 400 error if do not enter any value in mandatory fields', async ({ client }) => {
- const response = await client.post(API_URL).accept('json').end();
- response.assertStatus(400);
- });
- test('Should return created user if enter valid data for all mandatory fields', async ({ client }) => {
- const data = {
- username: 'unittest',
- email: 'unittest@unittest.com',
- password: '11111111',
- password_confirmation: '11111111'
- };
- const response = await client.post(API_URL).send(data).end();
- response.assertStatus(200);
- response.assertJSONSubset({
- status: 'success',
- data: {
- username: 'unittest',
- email: 'unittest@unittest.com'
- }
- });
- });
- test('Should throw 400 error if enter username with less than 8 characters', async ({ client }) => {
- const invalidData = {
- username: 'short'
- };
- const response = await client.post(API_URL).send(invalidData).end();
- response.assertStatus(400);
- response.assertError({
- "status": "fail",
- "data": [
- {
- "message": "min validation failed on username",
- "field": "username",
- "validation": "min"
- }
- ]
- });
- });
- test('Should throw 400 error if enter an already existing username', async ({ client }) => {
- const invalidData = {
- username: 'unittest'
- };
- const response = await client.post(API_URL).send(invalidData).end();
- response.assertStatus(400);
- response.assertError({
- "status": "fail",
- "data": [
- {
- "message": "unique validation failed on username",
- "field": "username",
- "validation": "unique"
- }
- ]
- });
- });
- test('Should throw 400 if enter a password with less than 6 characters', async ({ client }) => {
- const invalidData = {
- username: 'validuser',
- email: 'validemail@validemail.com',
- password: 'short'
- };
- const response = await client.post(API_URL).send(invalidData).end();
- response.assertStatus(400);
- response.assertError({
- "status": "fail",
- "data": [
- {
- "message": "min validation failed on password",
- "field": "password",
- "validation": "min"
- }
- ]
- });
- });
- test('Should throw 400 error if enter a password with more than 16 characters', async ({ client }) => {
- const invalidData = {
- username: 'validuser',
- email: 'validemail@validemail.com',
- password: 'toolongpassword'
- };
- const response = await client.post(API_URL).send(invalidData).end();
- response.assertStatus(400);
- response.assertError({
- "status": "fail",
- "data": [
- {
- "message": "max validation failed on password",
- "field": "password",
- "validation": "max"
- }
- ]
- });
- });
- test('Should throw 400 error if enter a password that does not match with confirmation password', async ({ client }) => {
- const invalidData = {
- username: 'validuser',
- email: 'validemail@validemail.com',
- password: 'password',
- password_confirmation: 'password_confirmation'
- };
- const response = await client.post(API_URL).send(invalidData).end();
- response.assertStatus(400);
- response.assertError({
- "status": "fail",
- "data": [
- {
- "message": "confirmed validation failed on password",
- "field": "password",
- "validation": "confirmed"
- }
- ]
- });
- });
- test('Should throw 400 error if enter invalid email', async ({ client }) => {
- const invalidData = {
- username: 'validuser',
- email: 'invalidemail'
- };
- const response = await client.post(API_URL).send(invalidData).end();
- response.assertStatus(400);
- response.assertError({
- "status": "fail",
- "data": [
- {
- "message": "email validation failed on email",
- "field": "email",
- "validation": "email"
- }
- ]
- });
- });
- test('Should throw 400 error if enter an already existing email address', async ({ client }) => {
- const invalidData = {
- username: 'validuser',
- email: 'unittest@unittest.com'
- };
- const response = await client.post(API_URL).send(invalidData).end();
- response.assertStatus(400);
- response.assertError({
- "status": "fail",
- "data": [
- {
- "message": "unique validation failed on email",
- "field": "email",
- "validation": "unique"
- }
- ]
- });
- });
Add Comment
Please, Sign In to add comment