|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'swagger_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Auth API', type: :request do |
| 6 | + path '/api/v1/signup' do |
| 7 | + post 'Register a new user' do |
| 8 | + tags 'Authentication' |
| 9 | + consumes 'application/json' |
| 10 | + produces 'application/json' |
| 11 | + |
| 12 | + parameter name: :user, in: :body, schema: { |
| 13 | + type: :object, |
| 14 | + properties: { |
| 15 | + name: { type: :string, example: 'John Doe' }, |
| 16 | + email: { type: :string, format: :email, example: 'john@example.com' }, |
| 17 | + password: { type: :string, minLength: 6, example: 'password123' }, |
| 18 | + password_confirmation: { type: :string, example: 'password123' } |
| 19 | + }, |
| 20 | + required: %w[name email password] |
| 21 | + } |
| 22 | + |
| 23 | + response '201', 'User created successfully' do |
| 24 | + schema type: :object, |
| 25 | + properties: { |
| 26 | + user: { '$ref' => '#/components/schemas/User' }, |
| 27 | + message: { type: :string, example: 'Account created successfully' } |
| 28 | + }, |
| 29 | + required: %w[user message] |
| 30 | + |
| 31 | + let(:user) { { name: 'John Doe', email: 'john@example.com', password: 'password123' } } |
| 32 | + run_test! |
| 33 | + end |
| 34 | + |
| 35 | + response '422', 'Validation errors' do |
| 36 | + schema '$ref' => '#/components/schemas/ValidationErrors' |
| 37 | + |
| 38 | + let(:user) { { name: '', email: 'invalid', password: '123' } } |
| 39 | + run_test! |
| 40 | + end |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + path '/api/v1/login' do |
| 45 | + post 'Authenticate user and receive token cookie' do |
| 46 | + tags 'Authentication' |
| 47 | + consumes 'application/json' |
| 48 | + produces 'application/json' |
| 49 | + |
| 50 | + parameter name: :credentials, in: :body, schema: { |
| 51 | + type: :object, |
| 52 | + properties: { |
| 53 | + email: { type: :string, format: :email, example: 'john@example.com' }, |
| 54 | + password: { type: :string, example: 'password123' } |
| 55 | + }, |
| 56 | + required: %w[email password] |
| 57 | + } |
| 58 | + |
| 59 | + response '200', 'Logged in successfully' do |
| 60 | + schema type: :object, |
| 61 | + properties: { |
| 62 | + user: { '$ref' => '#/components/schemas/User' }, |
| 63 | + message: { type: :string, example: 'Logged in successfully' } |
| 64 | + }, |
| 65 | + required: %w[user message] |
| 66 | + |
| 67 | + let(:existing_user) { User.create!(name: 'John', email: 'john@example.com', password: 'password123') } |
| 68 | + let(:credentials) { { email: existing_user.email, password: 'password123' } } |
| 69 | + run_test! |
| 70 | + end |
| 71 | + |
| 72 | + response '401', 'Invalid credentials' do |
| 73 | + schema '$ref' => '#/components/schemas/Error' |
| 74 | + |
| 75 | + let(:credentials) { { email: 'wrong@example.com', password: 'wrong' } } |
| 76 | + run_test! |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + path '/api/v1/logout' do |
| 82 | + delete 'Log out current user' do |
| 83 | + tags 'Authentication' |
| 84 | + produces 'application/json' |
| 85 | + security [ cookieAuth: [] ] |
| 86 | + |
| 87 | + response '200', 'Logged out successfully' do |
| 88 | + schema type: :object, |
| 89 | + properties: { |
| 90 | + message: { type: :string, example: 'Logged out successfully' } |
| 91 | + }, |
| 92 | + required: %w[message] |
| 93 | + |
| 94 | + let(:user) { User.create!(name: 'John', email: 'john@example.com', password: 'password123') } |
| 95 | + before { sign_in(user) } |
| 96 | + run_test! |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + path '/api/v1/me' do |
| 102 | + get 'Get current authenticated user' do |
| 103 | + tags 'Authentication' |
| 104 | + produces 'application/json' |
| 105 | + security [ cookieAuth: [] ] |
| 106 | + |
| 107 | + response '200', 'Current user retrieved' do |
| 108 | + schema '$ref' => '#/components/schemas/User' |
| 109 | + |
| 110 | + let(:user) { User.create!(name: 'John', email: 'john@example.com', password: 'password123') } |
| 111 | + before { sign_in(user) } |
| 112 | + run_test! |
| 113 | + end |
| 114 | + |
| 115 | + response '401', 'Not authenticated' do |
| 116 | + schema '$ref' => '#/components/schemas/Error' |
| 117 | + run_test! |
| 118 | + end |
| 119 | + end |
| 120 | + end |
| 121 | +end |
0 commit comments