Commit 895ca3a7 authored by Katarzyna Kobierska's avatar Katarzyna Kobierska

Add tests for API CI Lint

parent fb1c7254
......@@ -59,13 +59,6 @@ module Ci
expose :id, :variables
expose :pipeline, using: Commit, as: :commit
end
class Lint < Grape::Entity
expose :content
expose :status
expose :builds
expose :stages
end
end
end
end
......@@ -4,19 +4,34 @@ module Ci
before { authenticate! }
resources :lint do
post do
content = params[:content]
begin
response = {}
@content = params[:content]
if content
config_processor = Ci::GitlabCiYamlProcessor.new(content)
stages = config_processor.stages
builds = config_processor.builds
status = true
if @content
@config_processor = Ci::GitlabCiYamlProcessor.new(@content)
@stages = @config_processor.stages
@builds = @config_processor.builds
response = { status: status, stages: stages, builds: builds }
end
response = {
content: @content,
status: "syntax is correct"
}
stage_builds = @stages.each do |stage|
response["#{stage}"] = @builds.select { |build| build[:stage] == stage }
end
else
render_api_error!("Please provide content of .gitlab-ci.yml", 400)
end
response
response
rescue Ci::GitlabCiYamlProcessor::ValidationError, Psych::SyntaxError => e
error!({ content: @content, status: "syntax is incorrect", message: e.message })
end
end
end
end
......
......@@ -3,21 +3,48 @@ require 'spec_helper'
describe Ci::API::API do
include ApiHelpers
let(:content) do
let(:user) { create(:user) }
let(:yaml_content) do
File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml'))
end
describe "Builds API for Lint" do
describe 'POST /ci/lint' do
context "with valid .gitlab-ci.yaml content" do
context "authorized user" do
it "validate content" do
post ci_api('/lint'), { private_token: user.private_token, content: yaml_content }
describe 'POST /ci/lint' do
before { content }
expect(response).to have_http_status(201)
expect(json_response).to be_an Hash
expect(json_response['status']).to eq('syntax is correct')
end
end
context "unauthorized user" do
it "does not validate content" do
post ci_api('/lint'), { content: yaml_content }
context "with valid .gitlab-ci.yaml file" do
it "has success status" do
# binding.pry
expect(response).to have_content(true)
expect(response).to have_http_status(401)
end
end
end
context "with invalid .gitlab_ci.yml content" do
it "validate content" do
post ci_api('/lint'), { private_token: user.private_token, content: 'invalid content'}
expect(response).to have_http_status(500)
expect(json_response['status']).to eq('syntax is incorrect')
end
end
context "no content" do
it "shows error message" do
post ci_api('/lint'), { private_token: user.private_token }
expect(response).to have_http_status(400)
expect(json_response['message']).to eq('Please provide content of .gitlab-ci.yml')
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment