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
   end
11
   end
12
 
12
 
13
   def create
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
     if membership.save
19
     if membership.save
20
       render json: MembershipSerializer.new(membership).serializable_hash.to_json, status: :created
20
       render json: MembershipSerializer.new(membership).serializable_hash.to_json, status: :created
21
     else
21
     else
22
-      render json: { errors: membership.errors }, status: :unprocessable_entity
22
+      render json: membership.errors, status: :unprocessable_entity
23
     end
23
     end
24
   end
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
   private
39
   private
27
 
40
 
28
   def memberships_params
41
   def memberships_params
29
-    params.require(:membership).permit(:team_id, :member_id, :can_edit)
42
+    params.require(:membership).permit(:can_edit)
30
   end
43
   end
31
   
44
   
32
   def set_memberships
45
   def set_memberships
33
     @membership = Membership.find(params[:id])
46
     @membership = Membership.find(params[:id])
34
   end
47
   end
35
-
36
 end
48
 end

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

26
         render json: TeamSerializer.new(team).serializable_hash.to_json, status: :created
26
         render json: TeamSerializer.new(team).serializable_hash.to_json, status: :created
27
       end
27
       end
28
     else 
28
     else 
29
-      render json: { errors: team.errors }, status: :unprocessable_entity
29
+      render json: team.errors, status: :unprocessable_entity
30
     end
30
     end
31
   end
31
   end
32
 
32
 

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

1
 class Membership < ApplicationRecord
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
   validates_uniqueness_of :member, scope: :team, message: "user already part of this team"
7
   validates_uniqueness_of :member, scope: :team, message: "user already part of this team"
5
 
8
 
6
   belongs_to :member, class_name: 'User'
9
   belongs_to :member, class_name: 'User'

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

4
   setup do
4
   setup do
5
     @membership = memberships(:one)
5
     @membership = memberships(:one)
6
     @user = users(:one)
6
     @user = users(:one)
7
+    @user2 = users(:two)
7
     @team = teams(:one)
8
     @team = teams(:one)
9
+    @team2 = teams(:two)
8
   end
10
   end
9
 
11
 
10
   # INDEX
12
   # INDEX
45
   #   end
47
   #   end
46
   #   assert_response :created
48
   #   assert_response :created
47
   # end
49
   # end
48
-  
49
 
50
 
50
   test "should forbid create membership" do
51
   test "should forbid create membership" do
51
     assert_no_difference("Membership.count") do
52
     assert_no_difference("Membership.count") do
55
     end
56
     end
56
     assert_response :forbidden
57
     assert_response :forbidden
57
   end
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
 end
108
 end

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

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

Loading…
Cancel
Save