client.test.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // For more information about this file see https://dove.feathersjs.com/guides/cli/client.test.html
  2. import assert from 'assert'
  3. import axios from 'axios'
  4. import rest from '@feathersjs/rest-client'
  5. import authenticationClient from '@feathersjs/authentication-client'
  6. import { app } from '../src/app'
  7. import { createClient } from '../src/client'
  8. import type { UserData } from '../src/client'
  9. const port = app.get('port')
  10. const appUrl = `http://${app.get('host')}:${port}`
  11. describe('application client tests', () => {
  12. const client = createClient(rest(appUrl).axios(axios))
  13. before(async () => {
  14. await app.listen(port)
  15. })
  16. after(async () => {
  17. await app.teardown()
  18. })
  19. it('initialized the client', () => {
  20. assert.ok(client)
  21. })
  22. it('creates and authenticates a user with email and password', async () => {
  23. const userData: UserData = {
  24. email: 'someone@example.com',
  25. password: 'supersecret'
  26. }
  27. await client.service('users').create(userData)
  28. const { user, accessToken } = await client.authenticate({
  29. strategy: 'local',
  30. ...userData
  31. })
  32. assert.ok(accessToken, 'Created access token for user')
  33. assert.ok(user, 'Includes user in authentication data')
  34. assert.strictEqual(user.password, undefined, 'Password is hidden to clients')
  35. await client.logout()
  36. // Remove the test user on the server
  37. await app.service('users').remove(user.id)
  38. })
  39. })