Guest User

Untitled

a guest
Sep 12th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const { test, trait, before, after } = use('Test/Suite')('Api User Signup');
  4. const API_URL = '/api/v1/users';
  5. const userModel = use('App/Models/User');
  6.  
  7. trait('Test/ApiClient');
  8.  
  9. // executed before all the tests for a given suite
  10. before(async () => {
  11. await userModel.query().where('username', 'unittest').delete();
  12. });
  13.  
  14. // executed after all the tests for a given suite
  15. after(async () => {
  16. await userModel.query().where('username', 'unittest').delete();
  17. });
  18.  
  19. test('Should throw 400 error if do not enter any value in mandatory fields', async ({ client }) => {
  20. const response = await client.post(API_URL).accept('json').end();
  21.  
  22. response.assertStatus(400);
  23. });
  24.  
  25. test('Should return created user if enter valid data for all mandatory fields', async ({ client }) => {
  26. const data = {
  27. username: 'unittest',
  28. email: 'unittest@unittest.com',
  29. password: '11111111',
  30. password_confirmation: '11111111'
  31. };
  32.  
  33. const response = await client.post(API_URL).send(data).end();
  34.  
  35. response.assertStatus(200);
  36. response.assertJSONSubset({
  37. status: 'success',
  38. data: {
  39. username: 'unittest',
  40. email: 'unittest@unittest.com'
  41. }
  42. });
  43. });
  44.  
  45. test('Should throw 400 error if enter username with less than 8 characters', async ({ client }) => {
  46. const invalidData = {
  47. username: 'short'
  48. };
  49.  
  50. const response = await client.post(API_URL).send(invalidData).end();
  51.  
  52. response.assertStatus(400);
  53. response.assertError({
  54. "status": "fail",
  55. "data": [
  56. {
  57. "message": "min validation failed on username",
  58. "field": "username",
  59. "validation": "min"
  60. }
  61. ]
  62. });
  63. });
  64.  
  65. test('Should throw 400 error if enter an already existing username', async ({ client }) => {
  66. const invalidData = {
  67. username: 'unittest'
  68. };
  69.  
  70. const response = await client.post(API_URL).send(invalidData).end();
  71.  
  72. response.assertStatus(400);
  73. response.assertError({
  74. "status": "fail",
  75. "data": [
  76. {
  77. "message": "unique validation failed on username",
  78. "field": "username",
  79. "validation": "unique"
  80. }
  81. ]
  82. });
  83. });
  84.  
  85. test('Should throw 400 if enter a password with less than 6 characters', async ({ client }) => {
  86. const invalidData = {
  87. username: 'validuser',
  88. email: 'validemail@validemail.com',
  89. password: 'short'
  90. };
  91.  
  92. const response = await client.post(API_URL).send(invalidData).end();
  93.  
  94. response.assertStatus(400);
  95. response.assertError({
  96. "status": "fail",
  97. "data": [
  98. {
  99. "message": "min validation failed on password",
  100. "field": "password",
  101. "validation": "min"
  102. }
  103. ]
  104. });
  105. });
  106.  
  107. test('Should throw 400 error if enter a password with more than 16 characters', async ({ client }) => {
  108. const invalidData = {
  109. username: 'validuser',
  110. email: 'validemail@validemail.com',
  111. password: 'toolongpassword'
  112. };
  113.  
  114. const response = await client.post(API_URL).send(invalidData).end();
  115.  
  116. response.assertStatus(400);
  117. response.assertError({
  118. "status": "fail",
  119. "data": [
  120. {
  121. "message": "max validation failed on password",
  122. "field": "password",
  123. "validation": "max"
  124. }
  125. ]
  126. });
  127. });
  128.  
  129. test('Should throw 400 error if enter a password that does not match with confirmation password', async ({ client }) => {
  130. const invalidData = {
  131. username: 'validuser',
  132. email: 'validemail@validemail.com',
  133. password: 'password',
  134. password_confirmation: 'password_confirmation'
  135. };
  136.  
  137. const response = await client.post(API_URL).send(invalidData).end();
  138.  
  139. response.assertStatus(400);
  140. response.assertError({
  141. "status": "fail",
  142. "data": [
  143. {
  144. "message": "confirmed validation failed on password",
  145. "field": "password",
  146. "validation": "confirmed"
  147. }
  148. ]
  149. });
  150. });
  151.  
  152. test('Should throw 400 error if enter invalid email', async ({ client }) => {
  153. const invalidData = {
  154. username: 'validuser',
  155. email: 'invalidemail'
  156. };
  157.  
  158. const response = await client.post(API_URL).send(invalidData).end();
  159.  
  160. response.assertStatus(400);
  161. response.assertError({
  162. "status": "fail",
  163. "data": [
  164. {
  165. "message": "email validation failed on email",
  166. "field": "email",
  167. "validation": "email"
  168. }
  169. ]
  170. });
  171. });
  172.  
  173. test('Should throw 400 error if enter an already existing email address', async ({ client }) => {
  174. const invalidData = {
  175. username: 'validuser',
  176. email: 'unittest@unittest.com'
  177. };
  178.  
  179. const response = await client.post(API_URL).send(invalidData).end();
  180.  
  181. response.assertStatus(400);
  182. response.assertError({
  183. "status": "fail",
  184. "data": [
  185. {
  186. "message": "unique validation failed on email",
  187. "field": "email",
  188. "validation": "unique"
  189. }
  190. ]
  191. });
  192. });
Add Comment
Please, Sign In to add comment