Procházet zdrojové kódy

Add index and show defs to activities controller and test units

Lou před 3 roky
rodič
revize
4a29f3c7ce

+ 23
- 0
app/controllers/api/v1/activities_controller.rb Zobrazit soubor

@@ -0,0 +1,23 @@
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 Zobrazit soubor

@@ -3,6 +3,7 @@ Rails.application.routes.draw do
3 3
     namespace :v1 do
4 4
       resources :users
5 5
       resources :tokens, only: %i[create]
6
+      resources :activities
6 7
     end
7 8
   end
8 9
 end

+ 34
- 0
test/controllers/api/v1/activities_controller_test.rb Zobrazit soubor

@@ -0,0 +1,34 @@
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…
Zrušit
Uložit