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
     render json: TaskSerializer.new(@task).serializable_hash.to_json
10
     render json: TaskSerializer.new(@task).serializable_hash.to_json
11
   end
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
   private
24
   private
14
 
25
 
15
   def task_params
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
   end
28
   end
18
 
29
 
19
   def set_task
30
   def set_task

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

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

+ 1
- 1
db/schema.rb View File

33
   end
33
   end
34
 
34
 
35
   create_table "tasks", force: :cascade do |t|
35
   create_table "tasks", force: :cascade do |t|
36
-    t.string "name"
36
+    t.string "name", null: false
37
     t.text "description"
37
     t.text "description"
38
     t.integer "activity_id", null: false
38
     t.integer "activity_id", null: false
39
     t.integer "user_id", null: false
39
     t.integer "user_id", null: false

+ 1
- 1
db/seeds.rb View File

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

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

51
   test "should create activity" do
51
   test "should create activity" do
52
     assert_difference("Activity.count") do
52
     assert_difference("Activity.count") do
53
       post api_v1_activities_url,
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
       headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
55
       headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
56
       as: :json
56
       as: :json
57
     end
57
     end

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

34
     as: :json
34
     as: :json
35
     assert_response :forbidden
35
     assert_response :forbidden
36
   end
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
 end
67
 end

Loading…
Cancel
Save