Commit 7995a346 authored by Candy Tsai's avatar Candy Tsai

self_service: submit test from JSON upload

parent 8036d911
......@@ -12,9 +12,23 @@
<% end %>
<header class="well">
<h1>Request Test</h1>
<p>You can choose to request a by filling the form.</p>
<p>You can choose to request a test by either uploading a JSON file or by filling the form.</p>
</header>
<div class="col-xs-12">
<div class="page-header">
<h3>Upload JSON file</h3>
</div>
<form action="/selfservice/test/upload" method="post" enctype="multipart/form-data">
<div class="form-group">
<input name="tests" type="file" id="submit-json">
<p class="help-block">You can get an example JSON through exporting the filled-out form below.</p>
</div>
<div class="form-group">
<button type="submit" name="submit" class="btn btn-default" autocomplete="off">
Upload JSON File
</button>
</div>
</form>
<div class="page-header">
<h3>Test Request Form</h3>
</div>
......
......@@ -75,5 +75,44 @@ module Debci
raise 'Please select a suite' if suite == ''
raise 'Please select an architecture' if archs.empty?
end
post '/test/upload' do
begin
test_requests = JSON.parse(File.read(params[:tests][:tempfile]))
validate_json_submission(test_requests)
# request tests
test_requests.each do |request|
request['arch'].each do |arch|
request_tests(request['tests'], request['suite'], arch, @user)
end
end
rescue JSON::ParserError => error
halt(400, "Invalid JSON: #{error}")
rescue StandardError => error
@error_msg = error
halt(400, erb(:self_service_test))
else
201
end
end
def validate_json_submission(test_requests)
raise "Not an array" unless test_requests.is_a?(Array)
errors = []
test_requests.each_with_index do |request, index|
request_suite = request['suite']
errors.push("No suite at request index #{index}") if request_suite == ''
errors.push("Wrong suite (#{request_suite}) at request index #{index}, available suites: #{settings.suites.join(', ')}") unless settings.suites.include?(request_suite)
archs = request['arch'].reject(&:empty?)
errors.push("No archs are specified at request index #{index}") if archs.empty?
errors.push("Wrong archs (#{archs.join(', ')}) at request index #{index}, available archs: #{settings.archs.join(', ')}") if (settings.archs & archs).length != archs.length
request['tests'].each_with_index do |t, i|
errors.push("Invalid package name at request index #{index} and test index #{i}") unless valid_package_name?(t['package'])
end
end
raise errors.join('<br>') unless errors.empty?
end
end
end
......@@ -15,6 +15,13 @@ describe Debci::SelfService do
mock_server('/selfservice', SelfService)
end
def create_json_file(obj)
temp_test_file = Tempfile.new
temp_test_file.write(JSON.dump(obj))
temp_test_file.rewind
temp_test_file
end
let(:suite) { Debci.config.suite }
let(:arch) { Debci.config.arch }
......@@ -75,4 +82,72 @@ describe Debci::SelfService do
expect(last_response.status).to eq(400)
end
end
context 'upload json file' do
it 'submits a task succesfully on a valid json file upload' do
test_json = [
{
"suite": suite,
"arch": [arch],
"tests": [
{
"trigger": "testing",
"package": "autodep8",
"pin-packages": [["src:bar", "unstable"], ["foo", "src:bar", "stable"]]
}
]
}
]
test_file = create_json_file(test_json)
post '/selfservice/test/upload', { tests: Rack::Test::UploadedFile.new(test_file) }, 'SSL_CLIENT_S_DN_CN' => 'foo@bar.com'
expect(last_response.status).to eq(201)
job = Debci::Job.last
expect(job.package).to eq('autodep8')
expect(job.suite).to eq(suite)
end
it 'should return error with an invalid suite' do
test_json = [
{
# invalid suite
"suite": "xyz",
"arch": ["arm64", "amd64"],
"tests": [
{
"trigger": "testing",
"package": "autodep8",
"pin-packages": [["src:bar", "unstable"], ["foo", "src:bar", "stable"]]
}
]
}
]
test_file = create_json_file(test_json)
job_count = Debci::Job.count
post '/selfservice/test/upload', { tests: Rack::Test::UploadedFile.new(test_file) }, 'SSL_CLIENT_S_DN_CN' => 'foo@bar.com'
expect(last_response.status).to eq(400)
expect(Debci::Job.count).to eq(job_count)
end
it 'should return error with an invalid arch' do
test_json = [
{
"suite": "unstable",
# invalid arch
"arch": ["xyz", "amd64"],
"tests": [
{
"trigger": "testing",
"package": "autodep8",
"pin-packages": [["src:bar", "unstable"], ["foo", "src:bar", "stable"]]
}
]
}
]
test_file = create_json_file(test_json)
job_count = Debci::Job.count
post '/selfservice/test/upload', { tests: Rack::Test::UploadedFile.new(test_file) }, 'SSL_CLIENT_S_DN_CN' => 'foo@bar.com'
expect(last_response.status).to eq(400)
expect(Debci::Job.count).to eq(job_count)
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