Add VariableSerializer for Ci::Variable

This commit is contained in:
Matija Čupić 2018-01-30 02:01:53 +01:00
parent b48d8c8ad0
commit 9be519c199
No known key found for this signature in database
GPG Key ID: 4BAF84FFACD2E5DE
6 changed files with 35 additions and 5 deletions

View File

@ -7,7 +7,7 @@ class Projects::VariablesController < Projects::ApplicationController
variables = @project.variables
.map { |variable| variable.present(current_user: current_user) }
render status: :ok, json: { variables: variables }
render status: :ok, json: { variables: VariableSerializer.new.represent(variables) }
end
end
end
@ -19,7 +19,7 @@ class Projects::VariablesController < Projects::ApplicationController
variables = @project.variables
.map { |variable| variable.present(current_user: current_user) }
return render status: :ok, json: { variables: variables }
return render status: :ok, json: { variables: VariableSerializer.new.represent(variables) }
end
render status: :bad_request, json: @project.errors.full_messages

View File

@ -0,0 +1,7 @@
class VariableEntity < Grape::Entity
expose :id
expose :key
expose :value
expose :protected?, as: :protected
end

View File

@ -0,0 +1,3 @@
class VariableSerializer < BaseSerializer
entity VariableEntity
end

View File

@ -24,7 +24,7 @@ describe Projects::VariablesController do
it 'renders the ci_variable as json' do
subject
expect(response.body).to include(variable.to_json)
expect(response).to match_response_schema('variable')
end
end
@ -91,7 +91,7 @@ describe Projects::VariablesController do
it 'has all variables in response' do
subject
expect(response.body).to include(project.variables.reload.to_json)
expect(response).to match_response_schema('variable')
end
end
@ -120,7 +120,7 @@ describe Projects::VariablesController do
it 'has all variables in response' do
subject
expect(response.body).to include(project.variables.reload.to_json)
expect(json_response['variables'].count).to eq(0)
end
end
end

View File

@ -0,0 +1,6 @@
{
"id": "string",
"key": "string",
"value": "string",
"protected": "boolean"
}

View File

@ -0,0 +1,14 @@
require 'spec_helper'
describe VariableEntity do
let(:variable) { create(:ci_variable) }
let(:entity) { described_class.new(variable) }
describe '#as_json' do
subject { entity.as_json }
it 'contains required fields' do
expect(subject).to include(:id, :key, :value, :protected)
end
end
end