API de comptabilité horaire.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

users_controller_test.rb 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. require "test_helper"
  2. class Api::V1::UsersControllerTest < ActionDispatch::IntegrationTest
  3. setup do
  4. @user = users(:one)
  5. end
  6. #SHOW
  7. test "should show user" do
  8. get api_v1_user_url(@user), as: :json
  9. assert_response :success
  10. # Test to ensure response contains the correct email
  11. json_response = JSON.parse(self.response.body)
  12. assert_equal @user.email, json_response['email']
  13. end
  14. #CREATE
  15. test "should create user" do
  16. assert_difference('User.count') do
  17. post api_v1_users_url, params: { user: { email: 'test@test.org', username: 'new_user_name', password: '123456' } }, as: :json
  18. end
  19. assert_response :created
  20. end
  21. test "should not create user with taken email" do
  22. assert_no_difference('User.count') do
  23. post api_v1_users_url, params: { user: { email: @user.email, username: 'username_test', password: '123456' } }, as: :json
  24. end
  25. assert_response :unprocessable_entity
  26. end
  27. test "should not create user with taken username" do
  28. assert_no_difference('User.count') do
  29. post api_v1_users_url, params: { user: { email: "test@email.com", username: @user.username, password: '123456' } }, as: :json
  30. end
  31. assert_response :unprocessable_entity
  32. end
  33. #UPDATE
  34. test "should update user" do
  35. patch api_v1_user_url(@user),
  36. params: { user: { email: @user.email, password: '123456' } },
  37. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  38. as: :json
  39. assert_response :success
  40. end
  41. test "should forbid update user" do
  42. patch api_v1_user_url(@user),
  43. params: { user: { email: @user.email, password: '123456' } },
  44. as: :json
  45. assert_response :forbidden
  46. end
  47. #DESTROY
  48. test "should destroy user" do
  49. assert_difference('User.count', -1) do
  50. delete api_v1_user_url(@user),
  51. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  52. as: :json
  53. end
  54. assert_response :no_content
  55. end
  56. test "should forbid destroy user" do
  57. assert_no_difference('User.count') do
  58. delete api_v1_user_url(@user), as: :json
  59. end
  60. assert_response :forbidden
  61. end
  62. end