Browse Source

correction des tests unitaires pour la partie admin

Lou 2 years ago
parent
commit
440ad4abf2

+ 1
- 1
app/controllers/api/v1/users_controller.rb View File

@@ -55,6 +55,6 @@ class Api::V1::UsersController < ApplicationController
55 55
   end
56 56
 
57 57
   def check_owner_or_admin
58
-    head :forbidden unless @user.id == current_user&.id || current_user.admin
58
+    head :forbidden unless @user.id == current_user&.id || current_user&.admin
59 59
   end
60 60
 end

+ 23
- 1
test/controllers/api/v1/users_controller_test.rb View File

@@ -3,11 +3,27 @@ require "test_helper"
3 3
 class Api::V1::UsersControllerTest < ActionDispatch::IntegrationTest
4 4
   setup do
5 5
     @user = users(:one)
6
+    @admin = users(:admin)
7
+  end
8
+
9
+  # INDEX
10
+  test "should access index user" do
11
+    get api_v1_users_url,
12
+    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
13
+    as: :json
14
+    assert_response :success 
15
+  end
16
+
17
+  test "should forbid index user" do
18
+    get api_v1_users_url, as: :json
19
+    assert_response :forbidden 
6 20
   end
7 21
 
8 22
   #SHOW 
9 23
   test "should show user" do
10
-    get api_v1_user_url(@user), as: :json
24
+    get api_v1_user_url(@user),
25
+    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
26
+    as: :json
11 27
     assert_response :success
12 28
     # Test to ensure response contains the correct email
13 29
 
@@ -16,6 +32,12 @@ class Api::V1::UsersControllerTest < ActionDispatch::IntegrationTest
16 32
     assert_equal @user.username, json_response.dig(:data, :attributes, :username)
17 33
   end
18 34
 
35
+  test "should not show user" do
36
+    get api_v1_user_url(@user),
37
+    as: :json
38
+    assert_response :forbidden
39
+  end
40
+
19 41
   #CREATE
20 42
   test "should create user" do
21 43
     assert_difference('User.count') do

+ 3
- 1
test/fixtures/users.yml View File

@@ -3,9 +3,11 @@
3 3
 one:
4 4
   email: one@one.com
5 5
   username: OneUsername
6
+  admin: false
6 7
   password_digest: <%= BCrypt::Password.create('g00d_pa$$') %>
7 8
 
8 9
 two:
9 10
   email: two@two.com
10 11
   username: TwoUsername
11
-  password_digest: <%= BCrypt::Password.create('g00d_pa$$') %>
12
+  admin: false
13
+  password_digest: <%= BCrypt::Password.create('g00d_pa$$') %>

Loading…
Cancel
Save