Przeglądaj źródła

Adds serializer to activity controller, and unit tests to activity and user controllers

Lou 3 lat temu
rodzic
commit
9bc955cea8

+ 4
- 4
app/controllers/api/v1/activities_controller.rb Wyświetl plik

3
   before_action :check_login
3
   before_action :check_login
4
 
4
 
5
   def index
5
   def index
6
-    render json: Activity.all
6
+    render json: ActivitySerializer.new(Activity.all).serializable_hash.to_json
7
   end
7
   end
8
 
8
 
9
   def show
9
   def show
10
-    render json: Activity.find(params[:id])
10
+    render json: ActivitySerializer.new(@activity).serializable_hash.to_json
11
   end
11
   end
12
 
12
 
13
   def create
13
   def create
17
         user_id: current_user.id,
17
         user_id: current_user.id,
18
         activity_id: activity.id
18
         activity_id: activity.id
19
       )
19
       )
20
-      render json: activity, status: :created
20
+      render json: ActivitySerializer.new(activity).serializable_hash.to_json, status: :created
21
     else
21
     else
22
       render json: { errors: activity.errors }, status: :unprocessable_entity
22
       render json: { errors: activity.errors }, status: :unprocessable_entity
23
     end
23
     end
25
 
25
 
26
   def update
26
   def update
27
     if @activity.update(activity_params)
27
     if @activity.update(activity_params)
28
-      render json: @activity
28
+      render json: ActivitySerializer.new(@activity).serializable_hash.to_json
29
     else
29
     else
30
       render json: @activity.errors, status: :unprocessable_entity
30
       render json: @activity.errors, status: :unprocessable_entity
31
     end
31
     end

+ 4
- 0
app/serializers/activity_serializer.rb Wyświetl plik

1
+class ActivitySerializer
2
+  include JSONAPI::Serializer
3
+  attributes :name, :author_id, :description, :client
4
+end

+ 6
- 0
test/controllers/api/v1/activities_controller_test.rb Wyświetl plik

25
     headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
25
     headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
26
     as: :json
26
     as: :json
27
     assert_response :success
27
     assert_response :success
28
+
29
+    json_response = JSON.parse(self.response.body)
30
+    assert_equal @activity.name, json_response['data']['attributes']['name']
31
+    assert_equal @activity.author_id, json_response['data']['attributes']['author_id']
32
+    assert_equal @activity.description, json_response['data']['attributes']['description']
33
+    assert_equal @activity.client, json_response['data']['attributes']['client']
28
   end
34
   end
29
 
35
 
30
   test "should not access show" do
36
   test "should not access show" do

+ 2
- 0
test/controllers/api/v1/users_controller_test.rb Wyświetl plik

10
     get api_v1_user_url(@user), as: :json
10
     get api_v1_user_url(@user), as: :json
11
     assert_response :success
11
     assert_response :success
12
     # Test to ensure response contains the correct email
12
     # Test to ensure response contains the correct email
13
+
13
     json_response = JSON.parse(self.response.body)
14
     json_response = JSON.parse(self.response.body)
14
     assert_equal @user.email, json_response['data']['attributes']['email']
15
     assert_equal @user.email, json_response['data']['attributes']['email']
16
+    assert_equal @user.username, json_response['data']['attributes']['username']
15
   end
17
   end
16
 
18
 
17
   #CREATE
19
   #CREATE

Loading…
Anuluj
Zapisz