Compare commits
15 Commits
d4db64f404
...
5cec47efcc
| Author | SHA1 | Date |
|---|---|---|
|
|
5cec47efcc | |
|
|
cabb4daded | |
|
|
1e61a76ded | |
|
|
ac3e9f14cd | |
|
|
5ee9351362 | |
|
|
2964e06639 | |
|
|
d1a0fc0222 | |
|
|
4a82174526 | |
|
|
3ae20255f6 | |
|
|
f69c53bf29 | |
|
|
7d6c098047 | |
|
|
3421e18f21 | |
|
|
0dec27a01c | |
|
|
c45cf6ab51 | |
|
|
e50667b392 |
|
|
@ -25,7 +25,12 @@ pipeline {
|
||||||
}
|
}
|
||||||
stage('Run tests') {
|
stage('Run tests') {
|
||||||
steps {
|
steps {
|
||||||
sh 'cucumber features --format html --out coverage/cucumber.html'
|
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
|
||||||
|
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') {
|
||||||
|
|
@ -36,9 +41,9 @@ pipeline {
|
||||||
alwaysLinkToLastBuild: false,
|
alwaysLinkToLastBuild: false,
|
||||||
keepAll: true,
|
keepAll: true,
|
||||||
reportDir: 'coverage',
|
reportDir: 'coverage',
|
||||||
reportFiles: 'cucumber.html, index.html',
|
reportFiles: 'cucumber.html, spec.html, index.html',
|
||||||
reportName: 'Test Results',
|
reportName: 'Test Results',
|
||||||
reportTitles: 'Cucumber Results, Test Coverage'])
|
reportTitles: 'Cucumber Results, RSpec Results, Test Coverage'])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
34
app.rb
34
app.rb
|
|
@ -7,6 +7,8 @@ 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]
|
||||||
|
|
@ -27,7 +29,7 @@ class App < Roda
|
||||||
|
|
||||||
r.post "create" do
|
r.post "create" do
|
||||||
url = r.params['url']
|
url = r.params['url']
|
||||||
if url.empty?
|
if url.nil? or url.empty?
|
||||||
flash['message'] = "Please enter a valid URL";
|
flash['message'] = "Please enter a valid URL";
|
||||||
r.redirect '/'
|
r.redirect '/'
|
||||||
end
|
end
|
||||||
|
|
@ -36,9 +38,37 @@ 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'] + '/' + links.filter(:url => url).first[:code]
|
@new_link = 'http://' + request.env['HTTP_HOST'] + '/' + 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,26 +10,3 @@ 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"
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
# 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"
|
||||||
|
|
@ -0,0 +1,60 @@
|
||||||
|
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