Browse Source

Add index and show defs to activities controller and test units

Lou 3 years ago
parent
commit
4a29f3c7ce

+ 23
- 0
app/controllers/api/v1/activities_controller.rb View File

1
+class Api::V1::ActivitiesController < ApplicationController
2
+  before_action :set_activity, only: %i[show update destroy]
3
+  before_action :check_login
4
+  
5
+  def index
6
+    render json: Activity.all
7
+  end
8
+
9
+  def show
10
+    render json: Activity.find(params[:id])
11
+  end
12
+
13
+  private
14
+
15
+  # Only allow a trusted parameter "white list" through.
16
+  def activity_params
17
+    params.require(:user).permit(:name, :author_id, :description, :client)
18
+  end
19
+
20
+  def set_activity
21
+    @activity = Activity.find(params[:id])
22
+  end
23
+end

+ 1
- 0
config/routes.rb View File

3
     namespace :v1 do
3
     namespace :v1 do
4
       resources :users
4
       resources :users
5
       resources :tokens, only: %i[create]
5
       resources :tokens, only: %i[create]
6
+      resources :activities
6
     end
7
     end
7
   end
8
   end
8
 end
9
 end

+ 34
- 0
test/controllers/api/v1/activities_controller_test.rb View File

1
+require "test_helper"
2
+
3
+class Api::V1::ActivitiesControllerTest < ActionDispatch::IntegrationTest
4
+  setup do
5
+    @activity = activities(:one)
6
+  end
7
+  
8
+  #INDEX
9
+  test "should access index" do
10
+    get api_v1_activities_url,
11
+    headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
12
+    as: :json
13
+    assert_response :success
14
+  end
15
+
16
+  test "should not access index" do
17
+    get api_v1_activities_url, as: :json
18
+    assert_response :forbidden
19
+  end
20
+
21
+  #SHOW
22
+  test "should access show" do
23
+    get api_v1_activity_url(@activity),
24
+    headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
25
+    as: :json
26
+    assert_response :success
27
+  end
28
+
29
+  test "should not access show" do
30
+    get api_v1_activity_url(@activity),
31
+    as: :json
32
+    assert_response :forbidden
33
+  end
34
+end

Loading…
Cancel
Save