Skip to content

Commit 4b71cde

Browse files
authored
Merge pull request #15 from tuanle03/clr-75-create-api-registration
[CLR-75] Create API Registration
2 parents 6351c3b + 9efda7f commit 4b71cde

File tree

7 files changed

+116
-7
lines changed

7 files changed

+116
-7
lines changed

app/api/web/registrations_api.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module API
2+
class Web::RegistrationsAPI < Grape::API
3+
resource :registrations do
4+
desc 'Create a new user'
5+
params do
6+
requires :user, type: Hash do
7+
requires :email, type: String, desc: 'Email'
8+
requires :password, type: String, desc: 'Password'
9+
requires :password_confirmation, type: String, desc: 'Password confirmation'
10+
end
11+
end
12+
post do
13+
if params[:user][:password] != params[:user][:password_confirmation]
14+
return error!({ error: 'Password confirmation does not match' }, 400)
15+
end
16+
17+
if User.find_by(email: params[:user][:email])
18+
return error!({ error: 'Email has already been taken' }, 400)
19+
end
20+
21+
user = User.new(params[:user])
22+
if user.save
23+
token = user.generate_jwt
24+
status 200
25+
{
26+
success: true,
27+
token: token
28+
}
29+
else
30+
error!({ error: user.errors.full_messages }, 400)
31+
end
32+
end
33+
end
34+
end
35+
end

app/api/web/sessions_api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module API
22
class Web::SessionsAPI < Grape::API
3-
namespace :session do
3+
namespace :sessions do
44
desc 'Sign in and retrieve JWT token'
55
params do
66
requires :email, type: String, desc: 'User email'

app/api/web_api.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class WebAPI < Grape::API
66

77
mount Web::FeedbacksAPI
88
mount Web::SessionsAPI
9+
mount Web::RegistrationsAPI
910

1011
add_swagger_documentation(
1112
format: :json,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
require 'rack/test'
5+
6+
describe Web do
7+
include Rack::Test::Methods
8+
9+
def app
10+
Web::RegistrationsAPI
11+
end
12+
13+
describe 'POST /api/web/registrations' do
14+
context 'with valid registration parameters' do
15+
it 'creates a new user and returns a JWT token' do
16+
post '/web/registrations', {
17+
user: {
18+
email: 'new_user@example.com',
19+
password: 'password123',
20+
password_confirmation: 'password123'
21+
}
22+
}
23+
24+
expect(last_response.status).to eq(200)
25+
json = JSON.parse(last_response.body)
26+
expect(json['success']).to eq(true)
27+
expect(json['token']).to be_present
28+
end
29+
end
30+
31+
context 'when email already exists' do
32+
before do
33+
User.create(email: 'existing_user@example.com', password: 'password123')
34+
end
35+
36+
it 'returns an error message' do
37+
post '/web/registrations', {
38+
user: {
39+
email: 'existing_user@example.com',
40+
password: 'password123',
41+
password_confirmation: 'password123'
42+
}
43+
}
44+
45+
expect(last_response.status).to eq(400)
46+
json = JSON.parse(last_response.body)
47+
expect(json['error']).to eq('Email has already been taken')
48+
end
49+
end
50+
51+
context 'when password confirmation does not match' do
52+
it 'returns an error message' do
53+
post '/web/registrations', {
54+
user: {
55+
email: 'user@example.com',
56+
password: 'password123',
57+
password_confirmation: 'password456'
58+
}
59+
}
60+
61+
expect(last_response.status).to eq(400)
62+
json = JSON.parse(last_response.body)
63+
expect(json['error']).to eq('Password confirmation does not match')
64+
end
65+
end
66+
end
67+
end

spec/api/web/sessions_api_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ def app
1010
Web::SessionsAPI
1111
end
1212

13-
describe 'POST api/web/session/sign_in' do
13+
describe 'POST api/web/sessions/sign_in' do
1414
let(:user) { create(:user, email: 'test@example.com', password: 'password123') }
1515

1616
context 'with valid credentials' do
1717
it 'returns a JWT token' do
18-
post '/web/session/sign_in', { email: user.email, password: 'password123' }
18+
post '/web/sessions/sign_in', { email: user.email, password: 'password123' }
1919

2020
expect(last_response.status).to eq(200)
2121
json = JSON.parse(last_response.body)
@@ -26,7 +26,7 @@ def app
2626

2727
context 'with invalid credentials' do
2828
it 'returns an error message' do
29-
post '/web/session/sign_in', { email: user.email, password: 'wrong_password' }
29+
post '/web/sessions/sign_in', { email: user.email, password: 'wrong_password' }
3030

3131
expect(last_response.status).to eq(401)
3232
json = JSON.parse(last_response.body)
@@ -36,13 +36,13 @@ def app
3636
end
3737
end
3838

39-
describe 'DELETE /api/web/session/sign_out' do
39+
describe 'DELETE /api/web/sessions/sign_out' do
4040
let(:user) { create(:user) }
4141
let(:token) { user.generate_jwt }
4242

4343
context 'with a valid token' do
4444
it 'returns a success response' do
45-
delete '/web/session/sign_out', {}, 'HTTP_AUTHORIZATION' => "Bearer #{token}"
45+
delete '/web/sessions/sign_out', {}, 'HTTP_AUTHORIZATION' => "Bearer #{token}"
4646
expect(last_response.status).to eq(200)
4747
json = JSON.parse(last_response.body)
4848
expect(json['success']).to eq(true)
@@ -51,7 +51,7 @@ def app
5151

5252
context 'with an invalid token' do
5353
it 'returns an unauthorized response' do
54-
delete '/web/session/sign_out', {}, 'HTTP_AUTHORIZATION' => 'Bearer invalid_token'
54+
delete '/web/sessions/sign_out', {}, 'HTTP_AUTHORIZATION' => 'Bearer invalid_token'
5555
expect(last_response.status).to eq(401)
5656
end
5757
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
FactoryBot.define do
22
factory :user do
3+
first_name { Faker::Name.first_name }
4+
last_name { Faker::Name.last_name }
35
email { 'test@example.com' }
46
password { 'password123' }
57
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe AllowlistedJwt, type: :model do
4+
end

0 commit comments

Comments
 (0)