Browse Source

Adds destroy def to activity controller, and unit tests

Lou 3 years ago
parent
commit
f29e840f58

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

1
 class Api::V1::ActivitiesController < ApplicationController
1
 class Api::V1::ActivitiesController < ApplicationController
2
   before_action :set_activity, only: %i[show update destroy]
2
   before_action :set_activity, only: %i[show update destroy]
3
   before_action :check_login
3
   before_action :check_login
4
-  before_action :check_owner, only: %i[update]
4
+  before_action :check_owner, only: %i[update destroy]
5
 
5
 
6
   def index
6
   def index
7
     render json: Activity.all
7
     render json: Activity.all
28
     end
28
     end
29
   end
29
   end
30
 
30
 
31
+  def destroy
32
+    @activity.destroy
33
+    head 204
34
+  end
35
+
31
   private
36
   private
32
 
37
 
33
   # Only allow a trusted parameter "white list" through.
38
   # Only allow a trusted parameter "white list" through.

+ 28
- 2
test/controllers/api/v1/activities_controller_test.rb View File

62
     assert_response :success
62
     assert_response :success
63
   end
63
   end
64
 
64
 
65
-  test "should forbid update activity - not connected" do
65
+  test "should forbid update activity - not logged in" do
66
     patch api_v1_activity_url(@activity),
66
     patch api_v1_activity_url(@activity),
67
     params: { activity: { name: "Updated name" } },
67
     params: { activity: { name: "Updated name" } },
68
     as: :json
68
     as: :json
69
     assert_response :forbidden
69
     assert_response :forbidden
70
   end
70
   end
71
 
71
 
72
-  test "should forbid update activity - not owner" do
72
+  test "should forbid update activity - not owner or admin" do
73
     patch api_v1_activity_url(@activity),
73
     patch api_v1_activity_url(@activity),
74
     params: { activity: { name: "Updated name" } },
74
     params: { activity: { name: "Updated name" } },
75
     headers: { Authorization: JsonWebToken.encode(user_id: users(:two).id) },
75
     headers: { Authorization: JsonWebToken.encode(user_id: users(:two).id) },
76
     as: :json
76
     as: :json
77
     assert_response :forbidden
77
     assert_response :forbidden
78
   end
78
   end
79
+
80
+  #DESTROY
81
+  test "should destroy activity" do
82
+    assert_difference "Activity.count", -1 do
83
+    delete api_v1_activity_url(@activity),
84
+    headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
85
+    as: :json
86
+    end
87
+    assert_response :no_content
88
+  end
89
+
90
+  test "should forbid destroy activity - not logged in" do
91
+    assert_no_difference('Activity.count') do
92
+      delete api_v1_activity_url(@activity), as: :json
93
+    end
94
+    assert_response :forbidden
95
+  end
96
+
97
+  test "should forbid destroy activity - not owner or admin" do
98
+    assert_no_difference('Activity.count') do
99
+    delete api_v1_activity_url(@activity),
100
+    headers: { Authorization: JsonWebToken.encode(user_id: users(:two).id) },
101
+    as: :json
102
+    end
103
+    assert_response :forbidden
104
+  end
79
 end
105
 end

Loading…
Cancel
Save