Compare commits
No commits in common. "5cec47efcc4334b48c52c72bb7f0fa889e5e2814" and "d4db64f40463d496cfcf6f68e5580c6fcbe2611c" have entirely different histories.
5cec47efcc
...
d4db64f404
|
|
@ -25,12 +25,7 @@ pipeline {
|
||||||
}
|
}
|
||||||
stage('Run tests') {
|
stage('Run tests') {
|
||||||
steps {
|
steps {
|
||||||
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
|
sh 'cucumber features --format html --out coverage/cucumber.html'
|
||||||
sh 'cucumber features --format html --out coverage/cucumber.html'
|
|
||||||
}
|
|
||||||
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
|
|
||||||
sh 'rspec spec --format html --out coverage/spec.html'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stage('Report results') {
|
stage('Report results') {
|
||||||
|
|
@ -41,9 +36,9 @@ pipeline {
|
||||||
alwaysLinkToLastBuild: false,
|
alwaysLinkToLastBuild: false,
|
||||||
keepAll: true,
|
keepAll: true,
|
||||||
reportDir: 'coverage',
|
reportDir: 'coverage',
|
||||||
reportFiles: 'cucumber.html, spec.html, index.html',
|
reportFiles: 'cucumber.html, index.html',
|
||||||
reportName: 'Test Results',
|
reportName: 'Test Results',
|
||||||
reportTitles: 'Cucumber Results, RSpec Results, Test Coverage'])
|
reportTitles: 'Cucumber Results, Test Coverage'])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
34
app.rb
34
app.rb
|
|
@ -7,8 +7,6 @@ class App < Roda
|
||||||
plugin :sessions, secret: ENV.delete('APP_SESSION_SECRET')
|
plugin :sessions, secret: ENV.delete('APP_SESSION_SECRET')
|
||||||
plugin :render, escape: true
|
plugin :render, escape: true
|
||||||
plugin :flash
|
plugin :flash
|
||||||
plugin :json_parser
|
|
||||||
plugin :request_headers
|
|
||||||
|
|
||||||
DB = Sequel.sqlite("db/#{ENV['DB_NAME']}")
|
DB = Sequel.sqlite("db/#{ENV['DB_NAME']}")
|
||||||
links = DB[:links]
|
links = DB[:links]
|
||||||
|
|
@ -29,7 +27,7 @@ class App < Roda
|
||||||
|
|
||||||
r.post "create" do
|
r.post "create" do
|
||||||
url = r.params['url']
|
url = r.params['url']
|
||||||
if url.nil? or url.empty?
|
if url.empty?
|
||||||
flash['message'] = "Please enter a valid URL";
|
flash['message'] = "Please enter a valid URL";
|
||||||
r.redirect '/'
|
r.redirect '/'
|
||||||
end
|
end
|
||||||
|
|
@ -38,37 +36,9 @@ class App < Roda
|
||||||
links.insert(url: url, code: code)
|
links.insert(url: url, code: code)
|
||||||
@message = "Link created"
|
@message = "Link created"
|
||||||
end
|
end
|
||||||
code = links.filter(:url => url).first[:code]
|
|
||||||
@message ||= "Link exists"
|
@message ||= "Link exists"
|
||||||
@new_link = 'http://' + request.env['HTTP_HOST'] + '/' + code
|
@new_link = 'http://' + request.env['HTTP_HOST'] + '/' + links.filter(:url => url).first[:code]
|
||||||
view :create
|
view :create
|
||||||
end
|
end
|
||||||
r.on "links" do
|
|
||||||
r.post do
|
|
||||||
if 'application/json' != r.headers['CONTENT_TYPE']
|
|
||||||
return {message: "Not a valid JSON request"}.to_json
|
|
||||||
end
|
|
||||||
|
|
||||||
url = r.params['url']
|
|
||||||
if url.nil?
|
|
||||||
response.status = 400
|
|
||||||
return {message: "missing url parameter"}.to_json
|
|
||||||
end
|
|
||||||
|
|
||||||
if url.empty?
|
|
||||||
response.status = 400
|
|
||||||
return {message: "invalid url parameter"}.to_json
|
|
||||||
end
|
|
||||||
|
|
||||||
if links.filter(:url => url).first.nil?
|
|
||||||
code = SecureRandom.urlsafe_base64 4
|
|
||||||
links.insert(url: url, code: code)
|
|
||||||
end
|
|
||||||
|
|
||||||
code = links.filter(:url => url).first[:code]
|
|
||||||
@new_link = 'http://' + request.env['HTTP_HOST'] + '/' + code
|
|
||||||
return {url: url, code: code, link: @new_link}.to_json
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -10,3 +10,26 @@ Feature: Homepage
|
||||||
And I should see the message "Enter a URL"
|
And I should see the message "Enter a URL"
|
||||||
And I should see a form field "url"
|
And I should see a form field "url"
|
||||||
And I should see a "Submit" button
|
And I should see a "Submit" button
|
||||||
|
|
||||||
|
Scenario: Submitting the form without entering a URL
|
||||||
|
Given I visit the "/" page
|
||||||
|
When I click the "Submit" button
|
||||||
|
Then I should be on "/" page
|
||||||
|
And I should see the message "Please enter a valid URL"
|
||||||
|
|
||||||
|
@db-test
|
||||||
|
Scenario: Submitting the form with a correct URL
|
||||||
|
Given I visit the "/" page
|
||||||
|
When I type "http://google.com" in the "url" field
|
||||||
|
And I click the "Submit" button
|
||||||
|
Then I should be on "/create" page
|
||||||
|
And I should see the message "Link created"
|
||||||
|
|
||||||
|
@db-test
|
||||||
|
Scenario: Submitting the form with an existing URL
|
||||||
|
Given I visit the "/" page
|
||||||
|
And A link already exists with the url "http://google.com"
|
||||||
|
When I type "http://google.com" in the "url" field
|
||||||
|
And I click the "Submit" button
|
||||||
|
Then I should be on "/create" page
|
||||||
|
And I should see the message "Link exists"
|
||||||
|
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
# features/submit.feature
|
|
||||||
|
|
||||||
Feature: Submit
|
|
||||||
|
|
||||||
Submitting URL's on the homepage
|
|
||||||
|
|
||||||
Scenario: Submitting the form without entering a URL
|
|
||||||
Given I visit the "/" page
|
|
||||||
When I click the "Submit" button
|
|
||||||
Then I should be on "/" page
|
|
||||||
And I should see the message "Please enter a valid URL"
|
|
||||||
|
|
||||||
@db-test
|
|
||||||
Scenario: Submitting the form with a correct URL
|
|
||||||
Given I visit the "/" page
|
|
||||||
When I type "http://google.com" in the "url" field
|
|
||||||
And I click the "Submit" button
|
|
||||||
Then I should be on "/create" page
|
|
||||||
And I should see the message "Link created"
|
|
||||||
|
|
||||||
@db-test
|
|
||||||
Scenario: Submitting the form with an existing URL
|
|
||||||
Given I visit the "/" page
|
|
||||||
And A link already exists with the url "http://google.com"
|
|
||||||
When I type "http://google.com" in the "url" field
|
|
||||||
And I click the "Submit" button
|
|
||||||
Then I should be on "/create" page
|
|
||||||
And I should see the message "Link exists"
|
|
||||||
|
|
@ -1,60 +0,0 @@
|
||||||
require_relative '../.env'
|
|
||||||
ENV["DB_NAME"] = "test_#{ENV["DB_NAME"]}"
|
|
||||||
require_relative '../app'
|
|
||||||
require 'rubygems'
|
|
||||||
require 'roda'
|
|
||||||
require 'sequel'
|
|
||||||
require 'rspec'
|
|
||||||
require 'rack/test'
|
|
||||||
|
|
||||||
# DB initialization
|
|
||||||
Sequel.extension :migration
|
|
||||||
Sequel.sqlite("db/#{ENV['DB_NAME']}") do |db|
|
|
||||||
Sequel::Migrator.apply(db, "db/migrations")
|
|
||||||
end
|
|
||||||
|
|
||||||
def app
|
|
||||||
App
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "Submit API request to create new link" do
|
|
||||||
include Rack::Test::Methods
|
|
||||||
before :each do
|
|
||||||
@links = Sequel.sqlite("db/#{ENV['DB_NAME']}")[:links]
|
|
||||||
end
|
|
||||||
after :each do
|
|
||||||
@links.delete
|
|
||||||
end
|
|
||||||
it "should return link data in json format when a valid url is submitted" do
|
|
||||||
data = {
|
|
||||||
url: 'http://google.com'
|
|
||||||
}
|
|
||||||
post('/links', data.to_json, "CONTENT_TYPE" => "application/json")
|
|
||||||
expect(last_response).to be_ok
|
|
||||||
response_json = JSON.parse(last_response.body)
|
|
||||||
expect(response_json['url']).to eq(data[:url])
|
|
||||||
expect(response_json['code']).not_to eq(nil)
|
|
||||||
expect(response_json['link']).to include(response_json['code'])
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return with a 400 status and 'invalid url parameter' message when an empty url is submitted" do
|
|
||||||
data = {
|
|
||||||
url: ''
|
|
||||||
}
|
|
||||||
post('/links', data.to_json, "CONTENT_TYPE" => "application/json")
|
|
||||||
expect(last_response.status).to eq(400)
|
|
||||||
response_json = JSON.parse(last_response.body)
|
|
||||||
expect(response_json['message']).to eq('invalid url parameter')
|
|
||||||
|
|
||||||
end
|
|
||||||
it "should return with a 400 status and 'missing url parameter' message when an empty url is submitted" do
|
|
||||||
data = {
|
|
||||||
}
|
|
||||||
post('/links', data.to_json, "CONTENT_TYPE" => "application/json")
|
|
||||||
expect(last_response.status).to eq(400)
|
|
||||||
response_json = JSON.parse(last_response.body)
|
|
||||||
expect(response_json['message']).to eq('missing url parameter')
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue