login_spec.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. # Login and logout work only via controller, so use type: request.
  4. RSpec.describe Gql::Mutations::Login, type: :request do
  5. context 'when logging on' do
  6. let(:agent_password) { 'some_test_password' }
  7. let(:agent) { create(:agent, password: agent_password) }
  8. let(:query) do
  9. File.read(Rails.root.join('app/frontend/common/graphql/mutations/login.graphql'))
  10. end
  11. let(:password) { agent_password }
  12. let(:fingerprint) { Faker::Number.number(digits: 6).to_s }
  13. let(:variables) do
  14. {
  15. login: agent.login,
  16. password: password,
  17. fingerprint: fingerprint,
  18. }
  19. end
  20. let(:graphql_response) do
  21. post '/graphql', params: { query: query, variables: variables }, as: :json
  22. json_response
  23. end
  24. context 'with correct credentials' do
  25. it 'returns session data' do
  26. expect(graphql_response['data']['login']['sessionId']).to be_present
  27. end
  28. end
  29. context 'without CSRF token', allow_forgery_protection: true do
  30. it 'fails with error message' do
  31. expect(graphql_response['errors'][0]).to include('message' => 'CSRF token verification failed!')
  32. end
  33. it 'fails with error type' do
  34. expect(graphql_response['errors'][0]['extensions']).to include({ 'type' => 'Exceptions::NotAuthorized' })
  35. end
  36. end
  37. context 'with wrong password' do
  38. let(:password) { 'wrong' }
  39. it 'fails with error message' do
  40. expect(graphql_response['data']['login']['errors']).to eq(['Wrong login or password combination.'])
  41. end
  42. end
  43. context 'without fingerprint' do
  44. let(:fingerprint) { nil }
  45. it 'fails with error message' do
  46. expect(graphql_response['errors'][0]).to include('message' => 'Variable $fingerprint of type String! was provided invalid value')
  47. end
  48. # No error type available for GraphQL::ExecutionErrors.
  49. end
  50. end
  51. end