From b8e6df732c5f43c30aefd3ac7bf811292605cedc Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sat, 30 Nov 2024 14:24:02 +0100 Subject: [PATCH] Generate user model, document some endpoints (missing email verification) --- .../users/registrations_controller.rb | 4 ++ app/controllers/users/sessions_controller.rb | 8 ++++ app/models/user.rb | 2 +- config/routes.rb | 8 +++- .../20241130095753_devise_create_users.rb | 2 +- db/schema.rb | 4 +- spec/requests/expenses_spec.rb | 2 + spec/requests/groups_spec.rb | 1 + spec/requests/guests_spec.rb | 4 ++ spec/requests/schemas.rb | 11 +++++ spec/requests/users/registrations_spec.rb | 33 +++++++++++++ spec/requests/users/sessions_spec.rb | 47 +++++++++++++++++++ spec/swagger_helper.rb | 1 + spec/swagger_response_helper.rb | 23 +++++++++ 14 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 app/controllers/users/registrations_controller.rb create mode 100644 app/controllers/users/sessions_controller.rb create mode 100644 spec/requests/schemas.rb create mode 100644 spec/requests/users/registrations_spec.rb create mode 100644 spec/requests/users/sessions_spec.rb diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb new file mode 100644 index 0000000..538b5f0 --- /dev/null +++ b/app/controllers/users/registrations_controller.rb @@ -0,0 +1,4 @@ +class Users::RegistrationsController < Devise::RegistrationsController + clear_respond_to + respond_to :json +end \ No newline at end of file diff --git a/app/controllers/users/sessions_controller.rb b/app/controllers/users/sessions_controller.rb new file mode 100644 index 0000000..7d78323 --- /dev/null +++ b/app/controllers/users/sessions_controller.rb @@ -0,0 +1,8 @@ +class Users::SessionsController < Devise::SessionsController + clear_respond_to + respond_to :json + + # skip_before_action :authenticate_user!, only: %i[create] + + # skip_before_action :verify_authenticity_token, if: :development_swagger? +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index ff94b20..d338416 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,7 +4,7 @@ # # Table name: users # -# id :bigint not null, primary key +# id :uuid not null, primary key # confirmation_sent_at :datetime # confirmation_token :string # confirmed_at :datetime diff --git a/config/routes.rb b/config/routes.rb index 4000ade..ffd2772 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,13 @@ # Copyright (C) 2024 Manuel Bustillo Rails.application.routes.draw do - devise_for :users + devise_scope :user do + post 'users', to: 'users/registrations#create' + + post '/users/sign_in', to: 'users/sessions#create' + delete '/users/sign_out', to: 'users/sessions#destroy' + end + mount Rswag::Ui::Engine => '/api-docs' mount Rswag::Api::Engine => '/api-docs' resources :groups, only: :index diff --git a/db/migrate/20241130095753_devise_create_users.rb b/db/migrate/20241130095753_devise_create_users.rb index d1fb02d..977c72e 100644 --- a/db/migrate/20241130095753_devise_create_users.rb +++ b/db/migrate/20241130095753_devise_create_users.rb @@ -4,7 +4,7 @@ class DeviseCreateUsers < ActiveRecord::Migration[8.0] def change - create_table :users do |t| + create_table :users, id: :uuid do |t| ## Database authenticatable t.string :email, null: false, default: "" t.string :encrypted_password, null: false, default: "" diff --git a/db/schema.rb b/db/schema.rb index 45b3d87..d61fb3a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,5 +1,3 @@ -# Copyright (C) 2024 Manuel Bustillo - # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. @@ -188,7 +186,7 @@ ActiveRecord::Schema[8.0].define(version: 2024_11_30_095753) do t.string "name", null: false end - create_table "users", force: :cascade do |t| + create_table "users", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" diff --git a/spec/requests/expenses_spec.rb b/spec/requests/expenses_spec.rb index c96b5cf..5e99097 100644 --- a/spec/requests/expenses_spec.rb +++ b/spec/requests/expenses_spec.rb @@ -22,6 +22,7 @@ RSpec.describe 'expenses', type: :request do xit end + regular_api_responses end end @@ -44,6 +45,7 @@ RSpec.describe 'expenses', type: :request do response_empty_200 response_422 response_404 + regular_api_responses end end end diff --git a/spec/requests/groups_spec.rb b/spec/requests/groups_spec.rb index 597848f..fd014b5 100644 --- a/spec/requests/groups_spec.rb +++ b/spec/requests/groups_spec.rb @@ -28,6 +28,7 @@ RSpec.describe 'groups', type: :request do } xit end + regular_api_responses end end end diff --git a/spec/requests/guests_spec.rb b/spec/requests/guests_spec.rb index df26f17..b73fbcb 100644 --- a/spec/requests/guests_spec.rb +++ b/spec/requests/guests_spec.rb @@ -26,6 +26,7 @@ RSpec.describe 'guests', type: :request do } xit end + regular_api_responses end post('create guest') do @@ -50,6 +51,7 @@ RSpec.describe 'guests', type: :request do response_empty_201 response_422 + regular_api_responses end end @@ -77,6 +79,7 @@ RSpec.describe 'guests', type: :request do response_empty_200 response_422 response_404 + regular_api_responses end delete('delete guest') do @@ -86,6 +89,7 @@ RSpec.describe 'guests', type: :request do response_empty_200 response_404 + regular_api_responses end end end diff --git a/spec/requests/schemas.rb b/spec/requests/schemas.rb new file mode 100644 index 0000000..18e8548 --- /dev/null +++ b/spec/requests/schemas.rb @@ -0,0 +1,11 @@ +module Swagger + module Schema + USER = { + id: { type: :string, format: :uuid }, + email: { type: :string, format: :email }, + created_at: SwaggerResponseHelper::TIMESTAMP, + updated_at: SwaggerResponseHelper::TIMESTAMP + + } + end +end \ No newline at end of file diff --git a/spec/requests/users/registrations_spec.rb b/spec/requests/users/registrations_spec.rb new file mode 100644 index 0000000..aec3464 --- /dev/null +++ b/spec/requests/users/registrations_spec.rb @@ -0,0 +1,33 @@ +require 'swagger_helper' + +RSpec.describe 'users/registrations', type: :request do + + path '/users' do + post('create registration') do + tags 'Users Registrations' + consumes 'application/json' + produces 'application/json' + + parameter name: :body, in: :body, schema: { + type: :object, + required: [:user], + properties: { + user: { + type: :object, + required: %i[email password password_confirmation], + properties: { + email: { type: :string, format: :email}, + password: SwaggerResponseHelper::PASSWORD, + password_confirmation: SwaggerResponseHelper::PASSWORD + } + } + } + } + + response(201, 'created') do + schema type: :object, properties: Swagger::Schema::USER + xit + end + end + end +end diff --git a/spec/requests/users/sessions_spec.rb b/spec/requests/users/sessions_spec.rb new file mode 100644 index 0000000..2984ee9 --- /dev/null +++ b/spec/requests/users/sessions_spec.rb @@ -0,0 +1,47 @@ +require 'swagger_helper' + +RSpec.describe 'users/sessions', type: :request do + + path '/users/sign_in' do + + post('create session') do + tags 'Users Sessions' + consumes 'application/json' + produces 'application/json' + + parameter name: :body, in: :body, schema: { + type: :object, + required: %i[user], + properties: { + user: { + type: :object, + required: %i[email password], + properties: { + email: { type: :string, format: :email }, + password: SwaggerResponseHelper::PASSWORD + } + } + } + } + + response(201, 'created') do + schema type: :object, properties: Swagger::Schema::USER + xit + end + + response_401(message: 'Invalid Email or password.') + end + end + + path '/users/sign_out' do + + delete('delete session') do + tags 'Users Sessions' + consumes 'application/json' + produces 'application/json' + response(204, 'Session destroyed') do + xit + end + end + end +end diff --git a/spec/swagger_helper.rb b/spec/swagger_helper.rb index b87d934..2b19be8 100644 --- a/spec/swagger_helper.rb +++ b/spec/swagger_helper.rb @@ -4,6 +4,7 @@ require 'rails_helper' require_relative './swagger_response_helper' +require_relative './requests/schemas.rb' include SwaggerResponseHelper diff --git a/spec/swagger_response_helper.rb b/spec/swagger_response_helper.rb index 1f84ebd..d7f2e0e 100644 --- a/spec/swagger_response_helper.rb +++ b/spec/swagger_response_helper.rb @@ -1,6 +1,17 @@ # Copyright (C) 2024 Manuel Bustillo module SwaggerResponseHelper + TIMESTAMP_FORMAT = '\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z' + TIMESTAMP_EXAMPLE = Time.zone.now.iso8601(3) + + TIMESTAMP = {type: :string,pattern: TIMESTAMP_FORMAT,example: TIMESTAMP_EXAMPLE}.freeze + PASSWORD = { type: :string, minLength: User.password_length.begin, maxLength: User.password_length.end } + + + def regular_api_responses + response_401 + end + def response_422 response(422, 'Validation errors in input parameters') do produces 'application/json' @@ -33,6 +44,18 @@ module SwaggerResponseHelper end end + def response_401(message: nil) + response(401, 'Unauthorized') do + produces 'application/json' + schema type: :object, + required: %i[error], + properties: { + error: { type: :string, example: message || 'You need to sign in or sign up before continuing.' } + } + xit + end + end + private def error_schema