Browse Source

correction des tests unitaires pour la partie admin

Lou 3 years ago
parent
commit
440ad4abf2

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

55
   end
55
   end
56
 
56
 
57
   def check_owner_or_admin
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
   end
59
   end
60
 end
60
 end

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

3
 class Api::V1::UsersControllerTest < ActionDispatch::IntegrationTest
3
 class Api::V1::UsersControllerTest < ActionDispatch::IntegrationTest
4
   setup do
4
   setup do
5
     @user = users(:one)
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
   end
20
   end
7
 
21
 
8
   #SHOW 
22
   #SHOW 
9
   test "should show user" do
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
     assert_response :success
27
     assert_response :success
12
     # Test to ensure response contains the correct email
28
     # Test to ensure response contains the correct email
13
 
29
 
16
     assert_equal @user.username, json_response.dig(:data, :attributes, :username)
32
     assert_equal @user.username, json_response.dig(:data, :attributes, :username)
17
   end
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
   #CREATE
41
   #CREATE
20
   test "should create user" do
42
   test "should create user" do
21
     assert_difference('User.count') do
43
     assert_difference('User.count') do

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

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