Browse Source

Adds index and show defs, and relatives unit tests

Lou 3 years ago
parent
commit
825c392b37

+ 22
- 0
app/controllers/api/v1/tasks_controller.rb View File

@@ -0,0 +1,22 @@
1
+class Api::V1::TasksController < ApplicationController
2
+  before_action :set_task, only: %i[show update destroy]
3
+  before_action :check_login
4
+
5
+  def index
6
+    render json: TaskSerializer.new(Task.all).serializable_hash.to_json
7
+  end
8
+
9
+  def show
10
+    render json: TaskSerializer.new(@task).serializable_hash.to_json
11
+  end
12
+
13
+  private
14
+
15
+  def task_params
16
+    params.require(:task).permit(:name, :description, :user_id, :activity_id)
17
+  end
18
+
19
+  def set_task
20
+    @task = Task.find(params[:id])
21
+  end
22
+end

+ 6
- 0
app/serializers/task_serializer.rb View File

@@ -0,0 +1,6 @@
1
+class TaskSerializer
2
+  include JSONAPI::Serializer
3
+  attributes :name, :description, :user_id, :activity_id
4
+
5
+  cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
6
+end

BIN
erd.pdf View File


+ 37
- 0
test/controllers/api/v1/tasks_controller_test.rb View File

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

Loading…
Cancel
Save