Browse Source

en cours : ajout des methods update et destroy, et des tests unitaires

Lou 2 years ago
parent
commit
5de0989e3f

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

@@ -1,6 +1,7 @@
1 1
 class Api::V1::TeamsController < ApplicationController
2 2
   before_action :set_team, only: %i[show update destroy]
3 3
   before_action :check_login
4
+  before_action :can_edit?, only: %i[update destroy]
4 5
 
5 6
   def index
6 7
     render json: TeamSerializer.new(Team.all).serializable_hash.to_json
@@ -29,6 +30,19 @@ class Api::V1::TeamsController < ApplicationController
29 30
     end
30 31
   end
31 32
 
33
+  def update
34
+    if @team.update(team_params)
35
+      render json: TeamSerializer.new(@team).serializable_hash.to_json, status: :ok
36
+    else
37
+      render json: @team.errors, status: :unprocessable_entity
38
+    end
39
+  end
40
+
41
+  def destroy
42
+    @team.destroy
43
+    head 204
44
+  end
45
+
32 46
   private
33 47
   
34 48
   def team_params
@@ -39,4 +53,7 @@ class Api::V1::TeamsController < ApplicationController
39 53
     @team = Team.find(params[:id])
40 54
   end
41 55
 
56
+  def can_edit?
57
+    head :forbidden unless JoinedTeamUser.where(:team_id => params[:id]).where(:user_id => current_user.id)[0].can_edit
58
+  end
42 59
 end

+ 3
- 3
db/seeds.rb View File

@@ -13,13 +13,13 @@ JoinedTeamUser.reset_pk_sequence
13 13
 
14 14
 3.times do |i|
15 15
   team = Team.create(name: Faker::Company.name, description: Faker::Company.catch_phrase)
16
-  puts "Created TEAM ##{i} - #{team.name}"
16
+  puts "Created TEAM ##{i+1} - #{team.name}"
17 17
 end
18 18
 
19 19
 10.times do |i|
20 20
   name = Faker::Name.first_name.downcase
21 21
   user = User.create! username: "#{name}", email: "#{name}@email.com", password: "azerty"
22
-  puts "Created USER ##{i} - #{user.username}"
22
+  puts "Created USER ##{i+1} - #{user.username}"
23 23
   
24 24
   2.times do
25 25
     activity = Activity.create!(
@@ -56,6 +56,6 @@ puts "Created Admin"
56 56
     user_id: User.all.sample.id,
57 57
     activity_id: Activity.all.sample.id
58 58
   )
59
-  puts "Created TASK ##{i} - #{task.name}"
59
+  puts "Created TASK ##{i+1} - #{task.name}"
60 60
 end
61 61
 

BIN
erd.pdf View File


+ 54
- 8
test/controllers/api/v1/teams_controller_test.rb View File

@@ -27,7 +27,6 @@ class Api::V1::TeamsControllerTest < ActionDispatch::IntegrationTest
27 27
     assert_response :success
28 28
   end
29 29
 
30
-  # SHOW
31 30
   test "should forbid show team" do
32 31
     get api_v1_team_url(@team),
33 32
     as: :json
@@ -37,30 +36,77 @@ class Api::V1::TeamsControllerTest < ActionDispatch::IntegrationTest
37 36
   # CREATE
38 37
   test "should create team" do
39 38
     assert_difference('Team.count') do
40
-      post api_v1_teams_url, params: { team: { name: "Random name", description: "Random description" } }, headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, as: :json
39
+      post api_v1_teams_url,
40
+      params: { team: { name: "Random name", description: "Random description" } },
41
+      headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
42
+      as: :json
41 43
     end
42 44
     assert_response :created
43 45
   end
44 46
 
45
-  test "should not create team when not logged in" do
47
+  test "should forbid create team when not logged in" do
46 48
     assert_no_difference('Team.count') do
47
-      post api_v1_teams_url, params: { team: { name: "Random name", description: "Random description" } }, as: :json
49
+      post api_v1_teams_url,
50
+      params: { team: { name: "Random name", description: "Random description" } },
51
+      as: :json
48 52
     end
49 53
     assert_response :forbidden
50 54
   end
51 55
 
52
-  test "should not create team with taken name" do
56
+  test "should forbid create team with taken name" do
53 57
     assert_no_difference('Team.count') do
54
-      post api_v1_teams_url, params: { team: { name: @team.name, description: "Random description" } }, headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, as: :json
58
+      post api_v1_teams_url,
59
+      params: { team: { name: @team.name, description: "Random description" } },
60
+      headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
61
+      as: :json
55 62
     end
56 63
     assert_response :unprocessable_entity
57 64
   end
58 65
 
59
-  test "should not create team without name" do
66
+  test "should forbid create a team without name" do
60 67
     assert_no_difference('Team.count') do
61
-      post api_v1_teams_url, params: { team: { description: "Random description"} }, headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, as: :json
68
+      post api_v1_teams_url,
69
+      params: { team: { description: "Random description"} },
70
+      headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
71
+      as: :json
62 72
     end
63 73
     assert_response :unprocessable_entity
64 74
   end
65 75
   
76
+  # UPDATE
77
+  test "should update team" do
78
+    patch api_v1_team_url(@team),
79
+    params: { team: { name: "New name", description: "New description" } },
80
+    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
81
+    as: :json
82
+    assert_response :success
83
+  end
84
+
85
+  test "should not update team " do
86
+    patch api_v1_team_url(@team),
87
+    params: { team: { name: "New name", description: "New description" } },
88
+    as: :json
89
+    assert_response :forbidden
90
+  end
91
+
92
+  # Ajouter un test pour vérifier le statut de "current_user.can_edit" dans le modèle JoinedTeamUser
93
+
94
+  # DESTROY
95
+  test "should destroy team" do
96
+    assert_difference('Team.count', -1) do
97
+      delete api_v1_team_url(@team),
98
+      headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
99
+      as: :json
100
+    end
101
+    assert_response :no_content
102
+  end
103
+
104
+  test "should forbid destroy team" do
105
+    assert_no_difference('Team.count') do
106
+      delete api_v1_team_url(@team), as: :json
107
+    end
108
+    assert_response :forbidden
109
+  end
110
+  # Ajouter un test pour vérifier le statut de "current_user.can_edit" dans le modèle JoinedTeamUser
111
+
66 112
 end

+ 9
- 3
test/controllers/api/v1/users_controller_test.rb View File

@@ -40,21 +40,27 @@ class Api::V1::UsersControllerTest < ActionDispatch::IntegrationTest
40 40
   #CREATE
41 41
   test "should create user" do
42 42
     assert_difference('User.count') do
43
-      post api_v1_users_url, params: { user: { email: 'test@test.org', username: 'new_user_name', password: '123456' } }, as: :json
43
+      post api_v1_users_url,
44
+      params: { user: { email: 'test@test.org', username: 'new_user_name', password: '123456' } },
45
+      as: :json
44 46
     end
45 47
     assert_response :created
46 48
   end
47 49
 
48 50
   test "should not create user with taken email" do
49 51
     assert_no_difference('User.count') do
50
-      post api_v1_users_url, params: { user: { email: @user.email, username: 'username_test', password: '123456' } }, as: :json
52
+      post api_v1_users_url,
53
+      params: { user: { email: @user.email, username: 'username_test', password: '123456' } },
54
+      as: :json
51 55
     end
52 56
     assert_response :unprocessable_entity
53 57
   end
54 58
 
55 59
   test "should not create user with taken username" do
56 60
     assert_no_difference('User.count') do
57
-      post api_v1_users_url, params: { user: { email: "test@email.com", username: @user.username, password: '123456' } }, as: :json
61
+      post api_v1_users_url,
62
+      params: { user: { email: "test@email.com", username: @user.username, password: '123456' } },
63
+      as: :json
58 64
     end
59 65
     assert_response :unprocessable_entity
60 66
   end

+ 9
- 5
test/fixtures/joined_team_users.yml View File

@@ -4,8 +4,12 @@
4 4
 # model remove the '{}' from the fixture names and add the columns immediately
5 5
 # below each fixture, per the syntax in the comments below
6 6
 #
7
-one: {}
8
-# column: value
9
-#
10
-two: {}
11
-# column: value
7
+one:
8
+  team: one
9
+  user: one
10
+  can_edit: true
11
+
12
+two:
13
+  team: one
14
+  user: two
15
+  can_edit: false

+ 6
- 2
test/fixtures/teams.yml View File

@@ -1,5 +1,9 @@
1 1
 # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2 2
 
3 3
 one:
4
-  name: MyString
5
-  description: MyText
4
+  name: Team one
5
+  description: Team one
6
+
7
+one:
8
+  name: Team two
9
+  description: Team two

+ 6
- 0
test/fixtures/users.yml View File

@@ -11,3 +11,9 @@ two:
11 11
   username: TwoUsername
12 12
   is_admin: false
13 13
   password_digest: <%= BCrypt::Password.create('g00d_pa$$') %>
14
+
15
+admin:
16
+  email: admin@email.com
17
+  username: AdminUsername
18
+  is_admin: true
19
+  password_digest: <%= BCrypt::Password.create('g00d_pa$$') %>

Loading…
Cancel
Save