Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. const request = require('supertest')
  2. const app = require('../src/app')
  3. const User = require('../src/models/user')
  4.  
  5. const {
  6. userOne,
  7. userOneId,
  8. setupDatabase
  9. } = require('./fixtures/db')
  10.  
  11. beforeEach(setupDatabase)
  12.  
  13. test("Should signup a new user", async () => {
  14. const newUser = {
  15. name: "Me",
  16. email: "me@example.com",
  17. password: "red12345!"
  18. }
  19.  
  20. const response = await request(app).post('/users')
  21. .send(newUser)
  22. .expect(201)
  23.  
  24. // Assert user was correctly stored in the database.
  25. const user = await User.findById(response.body.user._id)
  26. expect(user).not.toBeNull()
  27.  
  28. // assertions about the resonse.
  29. expect(response.body).toMatchObject({
  30. user: {
  31. name: newUser.name,
  32. email: newUser.email
  33. },
  34. token: user.tokens[ 0 ].token
  35. })
  36.  
  37. // Check user password hashing.
  38. expect(user.password).not.toBe(newUser.password)
  39. })
  40.  
  41. test("Should log in existing user", async () => {
  42. const response = await request(app).post('/users/login')
  43. .send({
  44. email: userOne.email,
  45. password: userOne.password
  46. })
  47. .expect(200)
  48.  
  49. // Fetch the user from the database.
  50. const user = await User.findById(userOneId)
  51. expect(user).not.toBeNull()
  52. expect(user.tokens.length).toBe(2)
  53. expect(response.body.token).toBe(user.tokens[ 1 ].token)
  54. // expect(response.body.token).toBe(user.tokens[user.tokens.length - 1].token)
  55. })
  56.  
  57. test("Should not log in nonexistent user", async () => {
  58. await request(app).post('/users/login')
  59. .send({
  60. email: userOne.email,
  61. password: userOne.password + "XXX"
  62. })
  63. .expect(400)
  64. })
  65.  
  66. test("Should get profile for user", async () => {
  67. await request(app).get('/users/me')
  68. .set('Authorization', `Bearer ${userOne.tokens[ 0 ].token}`)
  69. .send()
  70. .expect(200)
  71. })
  72.  
  73. test("Should not get profile for non authenticated user", async () => {
  74. await request(app).get('/users/me')
  75. .send()
  76. .expect(401)
  77. })
  78.  
  79. test("Should delete account for user", async () => {
  80. const response = await request(app).delete('/users/me')
  81. .set('Authorization', `Bearer ${userOne.tokens[ 0 ].token}`)
  82. .send()
  83. .expect(200)
  84.  
  85. // Assert user was deleted.
  86. expect(userOneId.toString()).toBe(response.body._id)
  87. const user = await User.findById(userOneId)
  88. expect(user).toBeNull()
  89. })
  90.  
  91. test("Should not not delete account for non authenticated user", async () => {
  92. await request(app).delete('/users/me')
  93. .send()
  94. .expect(401)
  95. })
  96.  
  97. test("Should upload avatar image", async () => {
  98. await request(app).post('/users/me/avatar')
  99. .set('Authorization', `Bearer ${userOne.tokens[0].token}`)
  100. .attach('avatar', 'tests/fixtures/profile-pic.jpg')
  101. .expect(200)
  102. const user = await User.findById(userOneId)
  103. expect(user).not.toBeNull()
  104. expect(user.avatar).toEqual(expect.any(Buffer))
  105. })
  106.  
  107. test("Should update valid user fields", async () => {
  108. const newName = "New Name"
  109. await request(app).patch('/users/me')
  110. .set('Authorization', `Bearer ${userOne.tokens[0].token}`)
  111. .send({ name: newName })
  112. .expect(200)
  113. const user = await User.findById(userOneId)
  114. expect(user).not.toBeNull()
  115. expect(user.name).toBe(newName)
  116. })
  117.  
  118. test("Should not update invalid user fields", async () => {
  119. await request(app).patch('/users/me')
  120. .set('Authorization', `Bearer ${userOne.tokens[0].token}`)
  121. .send({ location: "New location" })
  122. .expect(400)
  123. })
  124.  
  125. //
  126. // User Test Ideas
  127. //
  128. // Should not signup user with invalid name/email/password
  129. // Should not update user if unauthenticated
  130. // Should not update user with invalid name/email/password
  131. // Should not delete user if unauthenticated
  132.  
  133. test("Should not signup user with invalid name/email/password", async () => {
  134. const emptyName = { name: "", email: "me@example.com", password: "red12345!" }
  135. const missingName = { email: "me@example.com", password: "red12345!" }
  136. const missingEmail = { name: "Me", password: "red12345!" }
  137. const missingPass = { name: "Me", email: "me@example.com" }
  138. const shortPass = { name: "Me", email: "me@example.com", password: "123"}
  139. const hasPass = { name: "Me", email: "me@example.com", password: "Apasswords"}
  140.  
  141. await request(app).post('/users').send(emptyName).expect(400)
  142. await request(app).post('/users').send(missingName).expect(400)
  143. await request(app).post('/users').send(missingEmail).expect(400)
  144. await request(app).post('/users').send(missingPass).expect(400)
  145. await request(app).post('/users').send(shortPass).expect(400)
  146. await request(app).post('/users').send(hasPass).expect(400)
  147. })
  148.  
  149. test("Should not update user if unauthenticated", async () => {
  150. await request(app).patch('/users/me')
  151. .send({ name: "Should Not Update" })
  152. .expect(401)
  153. })
  154.  
  155. test("Should not signup user with invalid name/email/password", async () => {
  156. const emptyName = { name: "" }
  157. const emptyEmail = { email: "" }
  158. const shortPass = { password: "123"}
  159. const hasPass = { password: "Apasswords"}
  160.  
  161. await request(app).patch('/users/me')
  162. .set('Authorization', `Bearer ${userOne.tokens[0].token}`)
  163. .send(emptyName)
  164. .expect(400)
  165. await request(app).patch('/users/me')
  166. .set('Authorization', `Bearer ${userOne.tokens[0].token}`)
  167. .send(emptyEmail)
  168. .expect(400)
  169. await request(app).patch('/users/me')
  170. .set('Authorization', `Bearer ${userOne.tokens[0].token}`)
  171. .send(shortPass)
  172. .expect(400)
  173. await request(app).patch('/users/me')
  174. .set('Authorization', `Bearer ${userOne.tokens[0].token}`)
  175. .send(hasPass)
  176. .expect(400)
  177. })
  178.  
  179. test("Should not delete user if unauthenticated", async () => {
  180. const response = await request(app).delete('/users/me')
  181. .send()
  182. .expect(401)
  183.  
  184. // Assert user was not deleted.
  185. const user = await User.findById(userOneId)
  186. expect(user).not.toBeNull()
  187. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement