Adds index and show defs, and relatives unit tests
This commit is contained in:
parent
5584e328b1
commit
825c392b37
4 changed files with 65 additions and 0 deletions
22
app/controllers/api/v1/tasks_controller.rb
Normal file
22
app/controllers/api/v1/tasks_controller.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
class Api::V1::TasksController < ApplicationController
|
||||
before_action :set_task, only: %i[show update destroy]
|
||||
before_action :check_login
|
||||
|
||||
def index
|
||||
render json: TaskSerializer.new(Task.all).serializable_hash.to_json
|
||||
end
|
||||
|
||||
def show
|
||||
render json: TaskSerializer.new(@task).serializable_hash.to_json
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def task_params
|
||||
params.require(:task).permit(:name, :description, :user_id, :activity_id)
|
||||
end
|
||||
|
||||
def set_task
|
||||
@task = Task.find(params[:id])
|
||||
end
|
||||
end
|
||||
6
app/serializers/task_serializer.rb
Normal file
6
app/serializers/task_serializer.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
class TaskSerializer
|
||||
include JSONAPI::Serializer
|
||||
attributes :name, :description, :user_id, :activity_id
|
||||
|
||||
cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
|
||||
end
|
||||
BIN
erd.pdf
BIN
erd.pdf
Binary file not shown.
37
test/controllers/api/v1/tasks_controller_test.rb
Normal file
37
test/controllers/api/v1/tasks_controller_test.rb
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
require "test_helper"
|
||||
|
||||
class Api::V1::TasksControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
@task = tasks(:one)
|
||||
@activity = activities(:one)
|
||||
@user = users(:one)
|
||||
end
|
||||
|
||||
# INDEX
|
||||
test "should access index" do
|
||||
get api_v1_activity_tasks_url(@activity),
|
||||
headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
|
||||
as: :json
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should not access index" do
|
||||
get api_v1_activity_tasks_url(@activity),
|
||||
as: :json
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
# SHOW
|
||||
test "should access show" do
|
||||
get api_v1_activity_task_url(@activity, @task),
|
||||
headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
|
||||
as: :json
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should not access show" do
|
||||
get api_v1_activity_task_url(@activity, @task),
|
||||
as: :json
|
||||
assert_response :forbidden
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue