Add def index/show/create to users controller, and test units

This commit is contained in:
Lou 2021-04-13 11:56:45 +02:00
commit d28c6612d0
2 changed files with 70 additions and 0 deletions

View file

@ -0,0 +1,38 @@
require "test_helper"
class Api::V1::UsersControllerTest < ActionDispatch::IntegrationTest
setup do
@user = users(:one)
end
#SHOW
test "should show user" do
get api_v1_user_url(@user), as: :json
assert_response :success
# Test to ensure response contains the correct email
json_response = JSON.parse(self.response.body)
assert_equal @user.email, json_response['email']
end
#CREATE
test "should create user" do
assert_difference('User.count') do
post api_v1_users_url, params: { user: { email: 'test@test.org', username: 'new_user_name', password: '123456' } }, as: :json
end
assert_response :created
end
test "should not create user with taken email" do
assert_no_difference('User.count') do
post api_v1_users_url, params: { user: { email: @user.email, username: 'username_test', password: '123456' } }, as: :json
end
assert_response :unprocessable_entity
end
test "should not create user with taken username" do
assert_no_difference('User.count') do
post api_v1_users_url, params: { user: { email: "test@email.com", username: @user.username, password: '123456' } }, as: :json
end
assert_response :unprocessable_entity
end
end