auth.service.spec.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @file Tests for the authentication service.
  3. * @author Inigo Valentin
  4. * @since 4.0.0
  5. */
  6. //require('../../node_modules/mysql2/node_modules/iconv-lite/lib').encodingExists('foo');
  7. const httpMocks = require('node-mocks-http');
  8. const jwt = require('jsonwebtoken');
  9. const dotenv = require('dotenv');
  10. dotenv.config({path: './env/test.env'});
  11. const AuthService = require("./auth.service.js");
  12. const authService = new AuthService();
  13. test('Valid login - correct username, email and password', () => {
  14. const req = {body: {email: "email@example.com", username: "username", password: "password"}};
  15. const res = httpMocks.createResponse();
  16. const testUser = User.build({
  17. 'email': 'email@example.com',
  18. 'username': 'username',
  19. 'password': '59b3e8d637cf97edbe2384cf59cb7453dfe30789',
  20. 'salt': 'salt'
  21. });
  22. jest.spyOn(User, 'findOne').mockImplementation((options) => Promise.resolve(testUser));
  23. authService.login(req, res).then(data => {expect(data.statusCode).toBe(200);});
  24. });
  25. test('Valid login - correct username and password, no email', () => {
  26. const req = {body: {username: "username", password: "password"}};
  27. const res = httpMocks.createResponse();
  28. const testUser = User.build({
  29. 'email': 'email@example.com',
  30. 'username': 'username',
  31. 'password': '59b3e8d637cf97edbe2384cf59cb7453dfe30789',
  32. 'salt': 'salt'
  33. });
  34. jest.spyOn(User, 'findOne').mockImplementation((options) => Promise.resolve(testUser));
  35. authService.login(req, res).then(data => {expect(data.statusCode).toBe(200);});
  36. });
  37. test('Valid login - correct email and password, no username', () => {
  38. const req = {body: {email: "email@example.com", password: "password"}};
  39. const res = httpMocks.createResponse();
  40. const testUser = User.build({
  41. 'email': 'email@example.com',
  42. 'username': 'username',
  43. 'password': '59b3e8d637cf97edbe2384cf59cb7453dfe30789',
  44. 'salt': 'salt'
  45. });
  46. jest.spyOn(User, 'findOne').mockImplementation((options) => Promise.resolve(testUser));
  47. authService.login(req, res).then(data => {expect(data.statusCode).toBe(200);});
  48. });
  49. test('Invalid login - no data', () => {
  50. const req = {body: {}};
  51. const res = httpMocks.createResponse();
  52. const testUser = User.build({
  53. 'email': 'email@example.com',
  54. 'username': 'username',
  55. 'password': '59b3e8d637cf97edbe2384cf59cb7453dfe30789',
  56. 'salt': 'salt'
  57. });
  58. jest.spyOn(User, 'findOne').mockImplementation((options) => Promise.resolve(testUser));
  59. authService.login(req, res).then(data => {expect(data.statusCode).toBe(401);});
  60. });
  61. test('Invalid login - correct username only', () => {
  62. const req = {body: {username: "username"}};
  63. const res = httpMocks.createResponse();
  64. const testUser = User.build({
  65. 'email': 'email@example.com',
  66. 'username': 'username',
  67. 'password': '59b3e8d637cf97edbe2384cf59cb7453dfe30789',
  68. 'salt': 'salt'
  69. });
  70. jest.spyOn(User, 'findOne').mockImplementation((options) => Promise.resolve(testUser));
  71. authService.login(req, res).then(data => {expect(data.statusCode).toBe(401);});
  72. });
  73. test('Invalid login - correct email only', () => {
  74. const req = {body: {email: "email@example.com"}};
  75. const res = httpMocks.createResponse();
  76. const testUser = User.build({
  77. 'email': 'email@example.com',
  78. 'username': 'username',
  79. 'password': '59b3e8d637cf97edbe2384cf59cb7453dfe30789',
  80. 'salt': 'salt'
  81. });
  82. jest.spyOn(User, 'findOne').mockImplementation((options) => Promise.resolve(testUser));
  83. authService.login(req, res).then(data => {expect(data.statusCode).toBe(401);});
  84. });
  85. test('Invalid login - corect password only', () => {
  86. const req = {body: {password: "password"}};
  87. const res = httpMocks.createResponse();
  88. const testUser = null;
  89. jest.spyOn(User, 'findOne').mockImplementation((options) => Promise.resolve(testUser));
  90. authService.login(req, res).then(data => {expect(data.statusCode).toBe(401);});
  91. });
  92. test('Invalid login - Correct username and email, wrong password', () => {
  93. const req = {body: {email: "email@example.com", username: "username", password: "BADpassword"}};
  94. const res = httpMocks.createResponse();
  95. const testUser = User.build({
  96. 'email': 'email@example.com',
  97. 'username': 'username',
  98. 'password': '59b3e8d637cf97edbe2384cf59cb7453dfe30789',
  99. 'salt': 'salt'
  100. });
  101. jest.spyOn(User, 'findOne').mockImplementation((options) => Promise.resolve(testUser));
  102. authService.login(req, res).then(data => {expect(data.statusCode).toBe(403);});
  103. });
  104. test('Valid login - Correct username and password, wrong email', () => {
  105. const req = {body: {email: "BADemail@example.com", username: "username", password: "password"}};
  106. const res = httpMocks.createResponse();
  107. const testUser = null;
  108. jest.spyOn(User, 'findOne').mockImplementation((options) => Promise.resolve(testUser));
  109. authService.login(req, res).then(data => {expect(data.statusCode).toBe(403);});
  110. });
  111. test('Valid login - Correct mail and password, wrong username', () => {
  112. const req = {body: {email: "email@example.com", username: "BADusername", password: "password"}};
  113. const res = httpMocks.createResponse();
  114. const testUser = null;
  115. jest.spyOn(User, 'findOne').mockImplementation((options) => Promise.resolve(testUser));
  116. authService.login(req, res).then(data => {expect(data.statusCode).toBe(403);});
  117. });
  118. test('Valid token in body', () => {
  119. const {TOKEN_SECRET, TOKEN_ISSUER} = process.env;
  120. const token = jwt.sign({user: 99}, TOKEN_SECRET, {algorithm: 'HS256', expiresIn: '10s', issuer: TOKEN_ISSUER, subject: "testuser"})
  121. const req = {body: {token: token}, query: {}, headers: {}};
  122. const res = httpMocks.createResponse();
  123. const validation = authService.validateToken(req, res);
  124. expect(validation.success).toBe(true);
  125. expect(validation.user).toBe(99);
  126. expect(validation.code).toBe(200);
  127. expect(validation.message).toBe("OK");
  128. });
  129. test('Valid token in request', () => {
  130. const {TOKEN_SECRET, TOKEN_ISSUER} = process.env;
  131. const token = jwt.sign({user: 99}, TOKEN_SECRET, {algorithm: 'HS256', expiresIn: '10s', issuer: TOKEN_ISSUER, subject: "testuser"})
  132. const req = {body: {}, query: {token: token}, headers: {}};
  133. const res = httpMocks.createResponse();
  134. const validation = authService.validateToken(req, res);
  135. expect(validation.success).toBe(true);
  136. expect(validation.user).toBe(99);
  137. expect(validation.code).toBe(200);
  138. expect(validation.message).toBe("OK");
  139. });
  140. test('Valid token in headers', () => {
  141. const {TOKEN_SECRET, TOKEN_ISSUER} = process.env;
  142. const token = jwt.sign({user: 99}, TOKEN_SECRET, {algorithm: 'HS256', expiresIn: '10s', issuer: TOKEN_ISSUER, subject: "testuser"})
  143. const req = {body: {}, query: {}, headers: {"x-access-token": token}};
  144. const res = httpMocks.createResponse();
  145. const validation = authService.validateToken(req, res);
  146. expect(validation.success).toBe(true);
  147. expect(validation.user).toBe(99);
  148. expect(validation.code).toBe(200);
  149. expect(validation.message).toBe("OK");
  150. });
  151. test('Invalid token in body', () => {
  152. const {TOKEN_SECRET, TOKEN_ISSUER} = process.env;
  153. const token = jwt.sign({user: 99}, TOKEN_SECRET, {algorithm: 'HS256', expiresIn: '10s', issuer: TOKEN_ISSUER, subject: "testuser"})
  154. const req = {body: {token: token + "BAD"}, query: {}, headers: {}};
  155. const res = httpMocks.createResponse();
  156. const validation = authService.validateToken(req, res);
  157. expect(validation.success).toBe(false);
  158. expect(validation.user).toBe(null);
  159. expect(validation.code).toBe(403);
  160. });
  161. test('Invalid token in request', () => {
  162. const {TOKEN_SECRET, TOKEN_ISSUER} = process.env;
  163. const token = jwt.sign({user: 99}, TOKEN_SECRET, {algorithm: 'HS256', expiresIn: '10s', issuer: TOKEN_ISSUER, subject: "testuser"})
  164. const req = {body: {}, query: {token: token+ "BAD"}, headers: {}};
  165. const res = httpMocks.createResponse();
  166. const validation = authService.validateToken(req, res);
  167. expect(validation.success).toBe(false);
  168. expect(validation.user).toBe(null);
  169. expect(validation.code).toBe(403);
  170. });
  171. test('Invalid token in headers', () => {
  172. const {TOKEN_SECRET, TOKEN_ISSUER} = process.env;
  173. const token = jwt.sign({user: 99}, TOKEN_SECRET, {algorithm: 'HS256', expiresIn: '10s', issuer: TOKEN_ISSUER, subject: "testuser"})
  174. const req = {body: {}, query: {}, headers: {"x-access-token": token + "BAD"}};
  175. const res = httpMocks.createResponse();
  176. const validation = authService.validateToken(req, res);
  177. expect(validation.success).toBe(false);
  178. expect(validation.user).toBe(null);
  179. expect(validation.code).toBe(403);
  180. });
  181. test('No token', () => {
  182. const {TOKEN_SECRET, TOKEN_ISSUER} = process.env;
  183. const token = jwt.sign({user: 99}, TOKEN_SECRET, {algorithm: 'HS256', expiresIn: '10s', issuer: TOKEN_ISSUER, subject: "testuser"})
  184. const req = {body: {}, query: {}, headers: {}};
  185. const res = httpMocks.createResponse();
  186. const validation = authService.validateToken(req, res);
  187. expect(validation.success).toBe(false);
  188. expect(validation.user).toBe(null);
  189. expect(validation.code).toBe(401);
  190. });