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

Testing/users #30

Merged
merged 5 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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: '[email protected]'
}

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: "[email protected]")
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