user.test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const mongoose = require('mongoose');
  2. const { getInstance } = require('../setup-crowi');
  3. describe('User', () => {
  4. // eslint-disable-next-line no-unused-vars
  5. let crowi;
  6. let User;
  7. beforeAll(async() => {
  8. crowi = await getInstance();
  9. User = mongoose.model('User');
  10. await User.create({
  11. name: 'Example for User Test',
  12. username: 'usertest',
  13. email: 'usertest@example.com',
  14. password: 'usertestpass',
  15. lang: 'en_US',
  16. });
  17. });
  18. describe('Create and Find.', () => {
  19. describe('The user', () => {
  20. test('should created with createUserByEmailAndPassword', (done) => {
  21. User.createUserByEmailAndPassword('Example2 for User Test', 'usertest2', 'usertest2@example.com', 'usertest2pass', 'en_US', (err, userData) => {
  22. expect(err).toBeNull();
  23. expect(userData).toBeInstanceOf(User);
  24. expect(userData.name).toBe('Example2 for User Test');
  25. done();
  26. });
  27. });
  28. test('should be found by findUserByUsername', async() => {
  29. const user = await User.findUserByUsername('usertest');
  30. expect(user).toBeInstanceOf(User);
  31. expect(user.name).toBe('Example for User Test');
  32. });
  33. });
  34. });
  35. describe('User Utilities', () => {
  36. describe('Get username from path', () => {
  37. test('found', () => {
  38. let username = null;
  39. username = User.getUsernameByPath('/user/sotarok');
  40. expect(username).toEqual('sotarok');
  41. username = User.getUsernameByPath('/user/some.user.name12/'); // with slash
  42. expect(username).toEqual('some.user.name12');
  43. });
  44. test('not found', () => {
  45. let username = null;
  46. username = User.getUsernameByPath('/the/page/is/not/related/to/user/page');
  47. expect(username).toBeNull();
  48. });
  49. });
  50. });
  51. });