Browse Source

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

Lou 3 years ago
parent
commit
5de0989e3f

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

1
 class Api::V1::TeamsController < ApplicationController
1
 class Api::V1::TeamsController < ApplicationController
2
   before_action :set_team, only: %i[show update destroy]
2
   before_action :set_team, only: %i[show update destroy]
3
   before_action :check_login
3
   before_action :check_login
4
+  before_action :can_edit?, only: %i[update destroy]
4
 
5
 
5
   def index
6
   def index
6
     render json: TeamSerializer.new(Team.all).serializable_hash.to_json
7
     render json: TeamSerializer.new(Team.all).serializable_hash.to_json
29
     end
30
     end
30
   end
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
   private
46
   private
33
   
47
   
34
   def team_params
48
   def team_params
39
     @team = Team.find(params[:id])
53
     @team = Team.find(params[:id])
40
   end
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
 end
59
 end

+ 3
- 3
db/seeds.rb View File

13
 
13
 
14
 3.times do |i|
14
 3.times do |i|
15
   team = Team.create(name: Faker::Company.name, description: Faker::Company.catch_phrase)
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
 end
17
 end
18
 
18
 
19
 10.times do |i|
19
 10.times do |i|
20
   name = Faker::Name.first_name.downcase
20
   name = Faker::Name.first_name.downcase
21
   user = User.create! username: "#{name}", email: "#{name}@email.com", password: "azerty"
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
   2.times do
24
   2.times do
25
     activity = Activity.create!(
25
     activity = Activity.create!(
56
     user_id: User.all.sample.id,
56
     user_id: User.all.sample.id,
57
     activity_id: Activity.all.sample.id
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
 end
60
 end
61
 
61
 

BIN
erd.pdf View File


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

27
     assert_response :success
27
     assert_response :success
28
   end
28
   end
29
 
29
 
30
-  # SHOW
31
   test "should forbid show team" do
30
   test "should forbid show team" do
32
     get api_v1_team_url(@team),
31
     get api_v1_team_url(@team),
33
     as: :json
32
     as: :json
37
   # CREATE
36
   # CREATE
38
   test "should create team" do
37
   test "should create team" do
39
     assert_difference('Team.count') do
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
     end
43
     end
42
     assert_response :created
44
     assert_response :created
43
   end
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
     assert_no_difference('Team.count') do
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
     end
52
     end
49
     assert_response :forbidden
53
     assert_response :forbidden
50
   end
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
     assert_no_difference('Team.count') do
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
     end
62
     end
56
     assert_response :unprocessable_entity
63
     assert_response :unprocessable_entity
57
   end
64
   end
58
 
65
 
59
-  test "should not create team without name" do
66
+  test "should forbid create a team without name" do
60
     assert_no_difference('Team.count') do
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
     end
72
     end
63
     assert_response :unprocessable_entity
73
     assert_response :unprocessable_entity
64
   end
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
 end
112
 end

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

40
   #CREATE
40
   #CREATE
41
   test "should create user" do
41
   test "should create user" do
42
     assert_difference('User.count') do
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
     end
46
     end
45
     assert_response :created
47
     assert_response :created
46
   end
48
   end
47
 
49
 
48
   test "should not create user with taken email" do
50
   test "should not create user with taken email" do
49
     assert_no_difference('User.count') do
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
     end
55
     end
52
     assert_response :unprocessable_entity
56
     assert_response :unprocessable_entity
53
   end
57
   end
54
 
58
 
55
   test "should not create user with taken username" do
59
   test "should not create user with taken username" do
56
     assert_no_difference('User.count') do
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
     end
64
     end
59
     assert_response :unprocessable_entity
65
     assert_response :unprocessable_entity
60
   end
66
   end

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

4
 # model remove the '{}' from the fixture names and add the columns immediately
4
 # model remove the '{}' from the fixture names and add the columns immediately
5
 # below each fixture, per the syntax in the comments below
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
 # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
1
 # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
 
2
 
3
 one:
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
   username: TwoUsername
11
   username: TwoUsername
12
   is_admin: false
12
   is_admin: false
13
   password_digest: <%= BCrypt::Password.create('g00d_pa$$') %>
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