Bläddra i källkod

added test and method create

Lou 2 år sedan
förälder
incheckning
584c66bcb7

+ 9
- 8
app/controllers/api/v1/memberships_controller.rb Visa fil

@@ -14,15 +14,16 @@ class Api::V1::MembershipsController < ApplicationController
14 14
     render json: MembershipSerializer.new(@membership).serializable_hash.to_json
15 15
   end
16 16
 
17
-  # def create  
18
-  #   membership = current_user.memberships.build(membership_params)
17
+  def create  
18
+    membership = current_user.memberships.build(membership_params)
19
+    membership.team_id = params[:team_id]
19 20
   
20
-  #   if membership.save
21
-  #     render json: MembershipSerializer.new(membership).serializable_hash.to_json, status: :created
22
-  #   else
23
-  #     render json: membership.errors, status: :unprocessable_entity
24
-  #   end
25
-  # end
21
+    if membership.save
22
+      render json: MembershipSerializer.new(membership).serializable_hash.to_json, status: :created
23
+    else
24
+      render json: membership.errors, status: :unprocessable_entity
25
+    end
26
+  end
26 27
 
27 28
   def update
28 29
     if params[:membership][:can_edit] 

+ 18
- 18
test/controllers/api/v1/memberships_controller_test.rb Visa fil

@@ -47,25 +47,25 @@ class Api::V1::MembershipsControllerTest < ActionDispatch::IntegrationTest
47 47
     assert_response :forbidden
48 48
   end
49 49
 
50
-  # CREATE
51
-  # test "should create membership" do
52
-  #   assert_difference("Membership.count") do
53
-  #     post api_v1_memberships_url,
54
-  #     params: { membership: { team_id: @team2.id, member_id: @user2.id, can_edit: false } },
55
-  #     headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
56
-  #     as: :json
57
-  #   end
58
-  #   assert_response :created
59
-  # end
50
+  #CREATE
51
+  test "should create membership" do
52
+    assert_difference("Membership.count") do
53
+      post api_v1_team_memberships_url(@team),
54
+      params: { membership: { can_edit: false, member_id: @user2, team_id: @team } },
55
+      headers: { Authorization: JsonWebToken.encode(user_id: @user2.id) }, 
56
+      as: :json
57
+    end
58
+    assert_response :created
59
+  end
60 60
 
61
-  # test "should forbid create membership" do
62
-  #   assert_no_difference("Membership.count") do
63
-  #     post api_v1_memberships_url,
64
-  #     params: { membership: { team_id: @team.id, member_id: @user.id, can_edit: false } },
65
-  #     as: :json
66
-  #   end
67
-  #   assert_response :forbidden
68
-  # end
61
+  test "should forbid create membership" do
62
+    assert_no_difference("Membership.count") do
63
+      post api_v1_team_memberships_url(@team),
64
+      params: { membership: { can_edit: false } },
65
+      as: :json
66
+    end
67
+    assert_response :forbidden
68
+  end
69 69
 
70 70
   # # UPDATE
71 71
   # test "should update membership" do

+ 0
- 5
test/fixtures/memberships.yml Visa fil

@@ -8,8 +8,3 @@ one:
8 8
   team: one
9 9
   member: one
10 10
   can_edit: true
11
-
12
-two:
13
-  team: one
14
-  member: two
15
-  can_edit: false

Loading…
Avbryt
Spara