summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatija Čupić <matteeyah@gmail.com>2018-01-30 02:01:53 +0100
committerMatija Čupić <matteeyah@gmail.com>2018-02-05 18:58:19 +0100
commit9be519c199b01d4b4b1c69ec4d74a1e99345eb47 (patch)
tree117f601dda66d48671a3e83b7ed93f8a00c72805
parentb48d8c8ad0bb2874db6b4c9accb3bebd19e9f2c8 (diff)
downloadgitlab-ce-9be519c199b01d4b4b1c69ec4d74a1e99345eb47.tar.gz
Add VariableSerializer for Ci::Variable
-rw-r--r--app/controllers/projects/variables_controller.rb4
-rw-r--r--app/serializers/variable_entity.rb7
-rw-r--r--app/serializers/variable_serializer.rb3
-rw-r--r--spec/controllers/projects/variables_controller_spec.rb6
-rw-r--r--spec/fixtures/api/schemas/variable.json6
-rw-r--r--spec/serializers/variable_entity_spec.rb14
6 files changed, 35 insertions, 5 deletions
diff --git a/app/controllers/projects/variables_controller.rb b/app/controllers/projects/variables_controller.rb
index 58fa600eb34..b5635ca1b3b 100644
--- a/app/controllers/projects/variables_controller.rb
+++ b/app/controllers/projects/variables_controller.rb
@@ -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
diff --git a/app/serializers/variable_entity.rb b/app/serializers/variable_entity.rb
new file mode 100644
index 00000000000..d576745c073
--- /dev/null
+++ b/app/serializers/variable_entity.rb
@@ -0,0 +1,7 @@
+class VariableEntity < Grape::Entity
+ expose :id
+ expose :key
+ expose :value
+
+ expose :protected?, as: :protected
+end
diff --git a/app/serializers/variable_serializer.rb b/app/serializers/variable_serializer.rb
new file mode 100644
index 00000000000..32ae82ab51c
--- /dev/null
+++ b/app/serializers/variable_serializer.rb
@@ -0,0 +1,3 @@
+class VariableSerializer < BaseSerializer
+ entity VariableEntity
+end
diff --git a/spec/controllers/projects/variables_controller_spec.rb b/spec/controllers/projects/variables_controller_spec.rb
index ddba6cd83f4..8e5d6023b79 100644
--- a/spec/controllers/projects/variables_controller_spec.rb
+++ b/spec/controllers/projects/variables_controller_spec.rb
@@ -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
diff --git a/spec/fixtures/api/schemas/variable.json b/spec/fixtures/api/schemas/variable.json
new file mode 100644
index 00000000000..422b4738418
--- /dev/null
+++ b/spec/fixtures/api/schemas/variable.json
@@ -0,0 +1,6 @@
+{
+ "id": "string",
+ "key": "string",
+ "value": "string",
+ "protected": "boolean"
+}
diff --git a/spec/serializers/variable_entity_spec.rb b/spec/serializers/variable_entity_spec.rb
new file mode 100644
index 00000000000..effc0022633
--- /dev/null
+++ b/spec/serializers/variable_entity_spec.rb
@@ -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