diff options
-rw-r--r-- | app/controllers/api/user_accounts_controller.rb | 6 | ||||
-rw-r--r-- | spec/requests/user_accounts_controller/create_spec.rb | 18 |
2 files changed, 21 insertions, 3 deletions
diff --git a/app/controllers/api/user_accounts_controller.rb b/app/controllers/api/user_accounts_controller.rb index 6404dee..da18448 100644 --- a/app/controllers/api/user_accounts_controller.rb +++ b/app/controllers/api/user_accounts_controller.rb @@ -12,7 +12,11 @@ module Api def create @user_account = UserAccount.new(permitted_params.merge(role: 'regular')) - render json: generate_token, status: :ok and return if @user_account.save + if @user_account.save + render json: generate_token, status: :ok + @user_account.update(cart_id: Cart.create(user_account_id: @user_account.id).id) + return + end render json: { errors: @user_account.errors.as_json }, status: :unprocessable_entity end diff --git a/spec/requests/user_accounts_controller/create_spec.rb b/spec/requests/user_accounts_controller/create_spec.rb index 7b70268..61ba5b4 100644 --- a/spec/requests/user_accounts_controller/create_spec.rb +++ b/spec/requests/user_accounts_controller/create_spec.rb @@ -3,19 +3,33 @@ require 'rails_helper' RSpec.describe 'POST /api/user_accounts', type: :request do + let(:user) { build(:user_account) } + let(:route) { '/api/user_accounts' } + let(:params) do + { email: user.email, password: user.password, + first_name: user.first_name, last_name: user.last_name } + end + it_behaves_like 'a POST request' do let(:headers) { {} } - let(:route) { '/api/user_accounts' } let(:expected_error_messages) do ["can't be blank", 'is invalid', "can't be blank", "can't be blank"] end let(:desired_error_status) { 422 } let(:expected_text) { %w[token refresh] } - let(:user) { build(:user_account) } let(:params) do { email: user.email, password: user.password, first_name: user.first_name, last_name: user.last_name } end let(:wrong_params) { { email: 'notanemail', password: '', first_name: '', last_name: '' } } end + + context 'after creating a user' do + it 'should create a cart belonging to him' do + post(route, params:) + created_user = UserAccount.find_by(email: user.email) + expect { Cart.find(created_user.cart_id) }.not_to raise_error + 2 + 2 + end + end end |