Skip to content

Commit

Permalink
Merge pull request #30 from concertmate/testing/users
Browse files Browse the repository at this point in the history
Testing/users
  • Loading branch information
GBowman1 authored Sep 19, 2024
2 parents 2b9a349 + 97853e0 commit 9b5fb53
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
7 changes: 4 additions & 3 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ def create
end

def destroy
user = User.find(params[:id])
user = User.find_by(id: params[:id])

if user.destroy
if user
user.destroy
render json: { message: "User has been deleted" }, status: :no_content
else
render json: { errors: user.errors.full_messages }, status: :unprocessable_entity
render json: { errors: 'User not found' }, status: :not_found
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class User < ApplicationRecord
has_many :artists, through: :user_artists

validates :name, presence: true
validates :email, presence: true
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }

end
58 changes: 58 additions & 0 deletions spec/requests/api/v1/user_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@
expect(user[:data][:attributes][:email]).to eq(user_params[:email])
end

it 'cant create a user' do
user_params = {
name: '',
email: 'new_user@email.com'
}

post "/api/v1/users", params: { user: user_params }

user = JSON.parse(response.body, symbolize_names: true)

expect(response).to_not be_successful
expect(response.status).to eq(422)

expect(user).to be_a(Hash)
expect(user).to have_key(:errors)
expect(user[:errors]).to be_a(Array)

expect(user[:errors].first).to be_a(String)
expect(user[:errors].first).to eq("Name can't be blank")
end

it "updates an existing user" do
user_id = @user1.id
updated_name = 'Updated Name'
Expand All @@ -62,6 +83,27 @@
expect(user[:data][:attributes][:email]).to eq(@user1.email)
end

it 'cant update a user with invalid email' do
user = User.create(name: "Valid Name", email: "valid@email.com")
user_id = user.id
put "/api/v1/users/#{user_id}", params: { user: { email: "invalid-email" } }


expect(response).to_not be_successful
expect(response.status).to eq(422)


user = JSON.parse(response.body, symbolize_names: true)


expect(user).to be_a(Hash)
expect(user).to have_key(:errors)


expect(user[:errors].first).to be_a(String)
expect(user[:errors].first).to eq("Email is invalid")
end

it "destroys an existing user" do
user_id = @user1.id

Expand All @@ -73,6 +115,20 @@
expect(User.find_by(id: user_id)).to be_nil
end

it 'cant destroy a user that doesnt exist' do

delete "/api/v1/users/12345432"

expect(response).to_not be_successful
expect(response.status).to eq(404)
# require 'pry'; binding.pry
user = JSON.parse(response.body, symbolize_names: true)
expect(user).to be_a(Hash)
# require 'pry'; binding.pry
expect(user[:errors]).to be_a(String)
expect(user[:errors]).to eq("User not found")
end

it "sends all users" do
get "/api/v1/users"

Expand All @@ -92,4 +148,6 @@
expect(users[:data].last[:attributes][:name]).to eq(@user2.name)
expect(users[:data].last[:attributes][:email]).to eq(@user2.email)
end


end

0 comments on commit 9b5fb53

Please sign in to comment.