Browse Source

Adds def create and test units, must fix a test

Lou 3 years ago
parent
commit
bfda3e561b

+ 12
- 1
app/controllers/api/v1/tasks_controller.rb View File

@@ -10,10 +10,21 @@ class Api::V1::TasksController < ApplicationController
10 10
     render json: TaskSerializer.new(@task).serializable_hash.to_json
11 11
   end
12 12
 
13
+  def create
14
+    task = current_user.tasks.build(task_params)
15
+    task.activity_id = params[:activity_id]
16
+    if task.save
17
+      render json: TaskSerializer.new(task).serializable_hash.to_json,
18
+      status: :created
19
+    else
20
+      render json: { errors: activity.errors }, status: :unprocessable_entity
21
+    end
22
+  end
23
+
13 24
   private
14 25
 
15 26
   def task_params
16
-    params.require(:task).permit(:name, :description, :user_id, :activity_id)
27
+    params.require(:task).permit(:name, :description, :activity_id)
17 28
   end
18 29
 
19 30
   def set_task

+ 2
- 1
db/migrate/20210510133939_create_tasks.rb View File

@@ -1,7 +1,8 @@
1 1
 class CreateTasks < ActiveRecord::Migration[6.1]
2 2
   def change
3 3
     create_table :tasks do |t|
4
-      t.string :name, index: true
4
+      t.string :name, null: false
5
+      t.index :name
5 6
       t.text :description
6 7
       t.references :activity, null: false
7 8
       t.references :user, null: false

+ 1
- 1
db/schema.rb View File

@@ -33,7 +33,7 @@ ActiveRecord::Schema.define(version: 2021_05_10_133939) do
33 33
   end
34 34
 
35 35
   create_table "tasks", force: :cascade do |t|
36
-    t.string "name"
36
+    t.string "name", null: false
37 37
     t.text "description"
38 38
     t.integer "activity_id", null: false
39 39
     t.integer "user_id", null: false

+ 1
- 1
db/seeds.rb View File

@@ -29,7 +29,7 @@ Task.reset_pk_sequence
29 29
   end
30 30
 end
31 31
 
32
-10.times do |i|
32
+30.times do |i|
33 33
   task = Task.create(
34 34
     name: Faker::Fantasy::Tolkien.character,
35 35
     description: Faker::Fantasy::Tolkien.poem,

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

@@ -51,7 +51,7 @@ class Api::V1::ActivitiesControllerTest < ActionDispatch::IntegrationTest
51 51
   test "should create activity" do
52 52
     assert_difference("Activity.count") do
53 53
       post api_v1_activities_url,
54
-      params: { activity: { name: @activity.name, client: @activity.client, description: @activity.description, author_id: @user }},
54
+      params: { activity: { name: @activity.name, client: @activity.client, description: @activity.description, author_id: @user } },
55 55
       headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
56 56
       as: :json
57 57
     end

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

@@ -34,4 +34,34 @@ class Api::V1::TasksControllerTest < ActionDispatch::IntegrationTest
34 34
     as: :json
35 35
     assert_response :forbidden
36 36
   end
37
+
38
+  # CREATE
39
+  test "should create task" do
40
+    assert_difference("Task.count") do
41
+      post api_v1_activity_tasks_url(@activity),
42
+      params: { task: { name: @task.name, description: @task.description, user_id: @user, activity_id: @activity } },
43
+      headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
44
+      as: :json
45
+    end
46
+    assert_response :created
47
+  end
48
+
49
+  test "should forbid create task - not logged in" do
50
+    assert_no_difference("Task.count") do
51
+      post api_v1_activity_tasks_url(@activity),
52
+      params: { task: { name: @task.name, description: @task.description, user_id: @user, activity_id: @activity } },
53
+      as: :json
54
+    end
55
+    assert_response :forbidden
56
+  end
57
+
58
+  test "should forbid create task - missing user or activity id" do
59
+    assert_no_difference("Task.count") do
60
+      post api_v1_activity_tasks_url(@activity),
61
+      params: { task: { name: @task.name, description: @task.description } },
62
+      headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
63
+      as: :json
64
+    end
65
+    assert_response :forbidden
66
+  end
37 67
 end

Loading…
Cancel
Save