Browse Source

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

Lou 3 years ago
parent
commit
9bc955cea8

+ 4
- 4
app/controllers/api/v1/activities_controller.rb View File

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

+ 4
- 0
app/serializers/activity_serializer.rb View File

@@ -0,0 +1,4 @@
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 View File

@@ -25,6 +25,12 @@ class Api::V1::ActivitiesControllerTest < ActionDispatch::IntegrationTest
25 25
     headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
26 26
     as: :json
27 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 34
   end
29 35
 
30 36
   test "should not access show" do

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

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

Loading…
Cancel
Save