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.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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, symbolize_names: true)
  12. assert_equal @user.email, json_response.dig(:data, :attributes, :email)
  13. assert_equal @user.username, json_response.dig(:data, :attributes, :username)
  14. end
  15. #CREATE
  16. test "should create user" do
  17. assert_difference('User.count') do
  18. post api_v1_users_url, params: { user: { email: 'test@test.org', username: 'new_user_name', password: '123456' } }, as: :json
  19. end
  20. assert_response :created
  21. end
  22. test "should not create user with taken email" do
  23. assert_no_difference('User.count') do
  24. post api_v1_users_url, params: { user: { email: @user.email, username: 'username_test', password: '123456' } }, as: :json
  25. end
  26. assert_response :unprocessable_entity
  27. end
  28. test "should not create user with taken username" do
  29. assert_no_difference('User.count') do
  30. post api_v1_users_url, params: { user: { email: "test@email.com", username: @user.username, password: '123456' } }, as: :json
  31. end
  32. assert_response :unprocessable_entity
  33. end
  34. #UPDATE
  35. test "should update user" do
  36. patch api_v1_user_url(@user),
  37. params: { user: { email: @user.email, password: '123456' } },
  38. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  39. as: :json
  40. assert_response :success
  41. end
  42. test "should forbid update user" do
  43. patch api_v1_user_url(@user),
  44. params: { user: { email: @user.email, password: '123456' } },
  45. as: :json
  46. assert_response :forbidden
  47. end
  48. #DESTROY
  49. test "should destroy user" do
  50. assert_difference('User.count', -1) do
  51. delete api_v1_user_url(@user),
  52. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  53. as: :json
  54. end
  55. assert_response :no_content
  56. end
  57. test "should forbid destroy user" do
  58. assert_no_difference('User.count') do
  59. delete api_v1_user_url(@user), as: :json
  60. end
  61. assert_response :forbidden
  62. end
  63. end