Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

187434149 flow to create user #42

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
module Api
module V1
class UsersController < BaseController
include ActionController::Flash

def create
render :json => 'the create method of UsersController is not yet implemented'.to_json, status: 501
email = params[:email]

# Check if the user already exists by email
existing_user = User.find_by(email: email)
if existing_user
render json: { message: 'A user with this email already exists.' }, status: :conflict
return
end

# Create a new user with the given email
new_user = User.create(email: email)
cynthia-lixinyi marked this conversation as resolved.
Show resolved Hide resolved

if new_user.persisted?
render json: { message: 'User created successfully', user: new_user }, status: :created
else
render json: { message: 'Failed to create user', errors: new_user.errors.full_messages }, status: :unprocessable_entity
end
end

def index
Expand Down
6 changes: 5 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# app/models/user.rb
class User < ApplicationRecord
validates :email, presence: true, uniqueness: true
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP, message: 'must be a valid email address' }

# Relationship with LmsCredential
# Associasions

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Associations

has_many :lms_credentials, dependent: :destroy
has_many :user_to_courses

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a strange variable/class name, and isn't plural (which i'd expect for has_many). do you mean something like course_specific_users? it's worth taking time to think carefully about names, as once they are committed you're generally stuck with them

has_many :lms_credentials
Sep26 marked this conversation as resolved.
Show resolved Hide resolved
has_one :extensions
cynthia-lixinyi marked this conversation as resolved.
Show resolved Hide resolved
end
43 changes: 39 additions & 4 deletions spec/controllers/api/v1/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,45 @@
module Api
module V1
describe UsersController do
describe 'create' do
it 'throws a 501 error' do
post :create
expect(response.status).to eq(501)
describe 'POST #create' do
context 'when creating a new user' do
it 'creates the user successfully' do
post :create, params: { email: '[email protected]' }

expect(response).to have_http_status(:created)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is nitpicking but: technically you should proabbly put the post in a before :each block and have the 3 expectations be separate specs, since each spec is supposed to test only 1 behavior

expect(JSON.parse(response.body)['message']).to eq('User created successfully')
expect(User.exists?(email: '[email protected]')).to be_truthy
end
end

context 'when user with the same email already exists' do
before do
User.create(email: '[email protected]')
end

it 'returns an error message' do
post :create, params: { email: '[email protected]' }

expect(response).to have_http_status(:conflict)
expect(JSON.parse(response.body)['message']).to eq('A user with this email already exists.')
end
end

context 'when email is missing or invalid' do
it 'returns an error when email is missing' do
post :create, params: { email: '' }

expect(response).to have_http_status(:unprocessable_entity)
expect(JSON.parse(response.body)['message']).to eq('Failed to create user')
end

it 'returns an error when email is invalid' do
# Assuming you add email format validation
post :create, params: { email: 'invalid-email' }

expect(response).to have_http_status(:unprocessable_entity)
expect(JSON.parse(response.body)['message']).to eq('Failed to create user')
end
end
end

Expand Down
Loading