Browse Source

ajout des def update et destroy - les tests ne sont pas complets

Lou 3 years ago
parent
commit
a06caa1a88

+ 18
- 6
app/controllers/api/v1/memberships_controller.rb View File

@@ -11,26 +11,38 @@ class Api::V1::MembershipsController < ApplicationController
11 11
   end
12 12
 
13 13
   def create
14
-    memberId = User.where(username: params[:membership][:username])[0].id
15
-    teamId = Team.where(name: params[:membership][:team_name])[0].id
14
+    member_id = User.where(username: params[:membership][:username])[0].id
15
+    team_id = Team.where(name: params[:membership][:team_name])[0].id
16 16
 
17
-    membership = Membership.new(team_id: teamId, member_id: memberId, can_edit: false)
17
+    membership = Membership.new(team_id: team_id, member_id: member_id, can_edit: false)
18 18
     
19 19
     if membership.save
20 20
       render json: MembershipSerializer.new(membership).serializable_hash.to_json, status: :created
21 21
     else
22
-      render json: { errors: membership.errors }, status: :unprocessable_entity
22
+      render json: membership.errors, status: :unprocessable_entity
23 23
     end
24 24
   end
25 25
 
26
+  def update
27
+    if @membership.update(memberships_params)
28
+      render json: MembershipSerializer.new(@membership).serializable_hash.to_json, status: :ok
29
+    else
30
+      render json: @membership.errors, status: :unprocessable_entity
31
+    end
32
+  end
33
+
34
+  def destroy
35
+    @membership.destroy
36
+    head 204
37
+  end
38
+
26 39
   private
27 40
 
28 41
   def memberships_params
29
-    params.require(:membership).permit(:team_id, :member_id, :can_edit)
42
+    params.require(:membership).permit(:can_edit)
30 43
   end
31 44
   
32 45
   def set_memberships
33 46
     @membership = Membership.find(params[:id])
34 47
   end
35
-
36 48
 end

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

@@ -26,7 +26,7 @@ class Api::V1::TeamsController < ApplicationController
26 26
         render json: TeamSerializer.new(team).serializable_hash.to_json, status: :created
27 27
       end
28 28
     else 
29
-      render json: { errors: team.errors }, status: :unprocessable_entity
29
+      render json: team.errors, status: :unprocessable_entity
30 30
     end
31 31
   end
32 32
 

+ 5
- 2
app/models/membership.rb View File

@@ -1,6 +1,9 @@
1 1
 class Membership < ApplicationRecord
2
-  # validates :member, presence: :true
3
-  # validates :team, presence: :true
2
+  attr_readonly :member
3
+  attr_readonly :team
4
+
5
+  validates :member, presence: :true
6
+  validates :team, presence: :true
4 7
   validates_uniqueness_of :member, scope: :team, message: "user already part of this team"
5 8
 
6 9
   belongs_to :member, class_name: 'User'

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

@@ -4,7 +4,9 @@ class Api::V1::MembershipsControllerTest < ActionDispatch::IntegrationTest
4 4
   setup do
5 5
     @membership = memberships(:one)
6 6
     @user = users(:one)
7
+    @user2 = users(:two)
7 8
     @team = teams(:one)
9
+    @team2 = teams(:two)
8 10
   end
9 11
 
10 12
   # INDEX
@@ -45,7 +47,6 @@ class Api::V1::MembershipsControllerTest < ActionDispatch::IntegrationTest
45 47
   #   end
46 48
   #   assert_response :created
47 49
   # end
48
-  
49 50
 
50 51
   test "should forbid create membership" do
51 52
     assert_no_difference("Membership.count") do
@@ -55,4 +56,53 @@ class Api::V1::MembershipsControllerTest < ActionDispatch::IntegrationTest
55 56
     end
56 57
     assert_response :forbidden
57 58
   end
59
+
60
+  # UPDATE
61
+  test "should update membership" do
62
+    patch api_v1_membership_url(@membership),
63
+    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
64
+    params: { membership: { can_edit: true } },
65
+    as: :json
66
+    assert_response :success
67
+  end
68
+
69
+  test "should forbid update membership" do
70
+    patch api_v1_membership_url(@membership),
71
+    params: { membership: { can_edit: true } },
72
+    as: :json
73
+    assert_response :forbidden
74
+  end
75
+
76
+  # test "should forbid update read_only attributes - team_id" do
77
+  #   patch api_v1_membership_url(@membership),
78
+  #   headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
79
+  #   params: { membership: { team_id: @team2.id } },
80
+  #   as: :json
81
+  #   assert_response :unprocessable_entity
82
+  # end
83
+
84
+  # test "should forbid update read_only attributes - member_id" do
85
+  #   patch api_v1_membership_url(@membership),
86
+  #   headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
87
+  #   params: { membership: { member_id: @user2.id } },
88
+  #   as: :json
89
+  #   assert_response :unprocessable_entity
90
+  # end  
91
+
92
+  # DESTROY
93
+  test "should destroy membership" do
94
+    assert_difference('Membership.count', -1) do
95
+      delete api_v1_membership_url(@membership), 
96
+      headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
97
+      as: :json
98
+    end
99
+    assert_response :no_content
100
+  end
101
+
102
+  test "should forbid destroy membership" do
103
+    assert_no_difference('Membership.count') do
104
+      delete api_v1_membership_url(@membership), as: :json
105
+    end
106
+    assert_response :forbidden
107
+  end
58 108
 end

+ 5
- 0
test/models/membership_test.rb View File

@@ -2,6 +2,7 @@ require "test_helper"
2 2
 
3 3
 class MembershipTest < ActiveSupport::TestCase
4 4
   setup do
5
+    @membership = memberships(:one)
5 6
     @user = users(:one)
6 7
     @user2 = users(:two)
7 8
     @team = teams(:one)
@@ -28,4 +29,8 @@ class MembershipTest < ActiveSupport::TestCase
28 29
     membership = Membership.new(member_id: @user.id, team_id: @team.id, can_edit: true)
29 30
     assert_not membership.valid?
30 31
   end
32
+
33
+  test "membership team & member id should not be updated" do
34
+    # How to perfom this test ?
35
+  end
31 36
 end

Loading…
Cancel
Save