Browse Source

ajout du Teams controller et des methods index show create, avec les tests unitaires

Lou 2 years ago
parent
commit
2c4bd2e870

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

@@ -0,0 +1,42 @@
1
+class Api::V1::TeamsController < ApplicationController
2
+  before_action :set_team, only: %i[show update destroy]
3
+  before_action :check_login
4
+
5
+  def index
6
+    render json: TeamSerializer.new(Team.all).serializable_hash.to_json
7
+  end
8
+
9
+  def show
10
+    render json: TeamSerializer.new(@team).serializable_hash.to_json
11
+  end
12
+
13
+  def create
14
+    team = current_user.teams.build(team_params)
15
+
16
+    if team.save 
17
+      if current_user&.is_admin === false
18
+        JoinedTeamUser.create!(
19
+          user_id: current_user.id,
20
+          team_id: team.id,
21
+          can_edit: true
22
+        )
23
+        render json: TeamSerializer.new(team).serializable_hash.to_json, status: :created
24
+      else
25
+        render json: TeamSerializer.new(team).serializable_hash.to_json, status: :created
26
+      end
27
+    else 
28
+      render json: {errors: team.errors }, status: :unprocessable_entity
29
+    end
30
+  end
31
+
32
+  private
33
+  
34
+  def team_params
35
+    params.require(:team).permit(:name, :description, :created_at, :updated_at)
36
+  end
37
+
38
+  def set_team
39
+    @team = Team.find(params[:id])
40
+  end
41
+
42
+end

+ 6
- 0
app/serializers/team_serializer.rb View File

@@ -0,0 +1,6 @@
1
+class TeamSerializer
2
+  include JSONAPI::Serializer
3
+  attributes :name, :description
4
+
5
+  cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
6
+end

+ 1
- 0
config/routes.rb View File

@@ -3,6 +3,7 @@ Rails.application.routes.draw do
3 3
     namespace :v1 do
4 4
       resources :users
5 5
       resources :tokens, only: %i[create]
6
+      resources :teams
6 7
       resources :activities do
7 8
         resources :tasks
8 9
       end

+ 66
- 0
test/controllers/api/v1/teams_controller_test.rb View File

@@ -0,0 +1,66 @@
1
+require "test_helper"
2
+
3
+class Api::V1::TeamsControllerTest < ActionDispatch::IntegrationTest
4
+  setup do
5
+    @team = teams(:one)
6
+    @user = users(:one)
7
+  end
8
+
9
+  # INDEX
10
+  test "should access team index" do
11
+    get api_v1_teams_url,
12
+    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
13
+    as: :json
14
+    assert_response :success
15
+  end
16
+
17
+  test "should forbid team index" do
18
+    get api_v1_teams_url, as: :json
19
+    assert_response :forbidden
20
+  end
21
+
22
+  # SHOW
23
+  test "should show team" do
24
+    get api_v1_team_url(@team), 
25
+    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
26
+    as: :json
27
+    assert_response :success
28
+  end
29
+
30
+  # SHOW
31
+  test "should forbid show team" do
32
+    get api_v1_team_url(@team),
33
+    as: :json
34
+    assert_response :forbidden
35
+  end
36
+
37
+  # CREATE
38
+  test "should create team" do
39
+    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
41
+    end
42
+    assert_response :created
43
+  end
44
+
45
+  test "should not create team when not logged in" do
46
+    assert_no_difference('Team.count') do
47
+      post api_v1_teams_url, params: { team: { name: "Random name", description: "Random description" } }, as: :json
48
+    end
49
+    assert_response :forbidden
50
+  end
51
+
52
+  test "should not create team with taken name" do
53
+    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
55
+    end
56
+    assert_response :unprocessable_entity
57
+  end
58
+
59
+  test "should not create team without name" do
60
+    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
62
+    end
63
+    assert_response :unprocessable_entity
64
+  end
65
+  
66
+end

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

@@ -6,14 +6,14 @@ class Api::V1::UsersControllerTest < ActionDispatch::IntegrationTest
6 6
   end
7 7
 
8 8
   # INDEX
9
-  test "should access index user" do
9
+  test "should access user index" do
10 10
     get api_v1_users_url,
11 11
     headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
12 12
     as: :json
13 13
     assert_response :success 
14 14
   end
15 15
 
16
-  test "should forbid index user" do
16
+  test "should forbid user index" do
17 17
     get api_v1_users_url, as: :json
18 18
     assert_response :forbidden 
19 19
   end

Loading…
Cancel
Save