rubocop formatting fixes
parent
48ffdf560b
commit
5abf07d0c7
|
|
@ -1,8 +1,10 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'simplecov'
|
require 'simplecov'
|
||||||
SimpleCov.start
|
SimpleCov.start
|
||||||
|
|
||||||
require_relative '../.env'
|
require_relative '../.env'
|
||||||
ENV["DB_NAME"] = "test_#{ENV["DB_NAME"]}"
|
ENV['DB_NAME'] = "test_#{ENV.fetch('DB_NAME', nil)}"
|
||||||
require_relative '../app'
|
require_relative '../app'
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'roda'
|
require 'roda'
|
||||||
|
|
@ -12,48 +14,47 @@ require 'rack/test'
|
||||||
|
|
||||||
# DB initialization
|
# DB initialization
|
||||||
Sequel.extension :migration
|
Sequel.extension :migration
|
||||||
Sequel.sqlite("db/#{ENV['DB_NAME']}") do |db|
|
Sequel.sqlite("db/#{ENV.fetch('DB_NAME', nil)}") do |db|
|
||||||
Sequel::Migrator.apply(db, "db/migrations")
|
Sequel::Migrator.apply(db, 'db/migrations')
|
||||||
end
|
end
|
||||||
|
|
||||||
def app
|
def app
|
||||||
App
|
App
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "Submit API request to create new link" do
|
describe 'Submit API request to create new link' do
|
||||||
include Rack::Test::Methods
|
include Rack::Test::Methods
|
||||||
before :each do
|
before :each do
|
||||||
@links = Sequel.sqlite("db/#{ENV['DB_NAME']}")[:links]
|
@links = Sequel.sqlite("db/#{ENV.fetch('DB_NAME', nil)}")[:links]
|
||||||
end
|
end
|
||||||
after :each do
|
after :each do
|
||||||
@links.delete
|
@links.delete
|
||||||
end
|
end
|
||||||
it "should return link data in json format when a valid url is submitted" do
|
it 'should return link data in json format when a valid url is submitted' do
|
||||||
data = {
|
data = {
|
||||||
url: 'http://google.com'
|
url: 'http://google.com'
|
||||||
}
|
}
|
||||||
post('/links', data.to_json, "CONTENT_TYPE" => "application/json")
|
post('/links', data.to_json, 'CONTENT_TYPE' => 'application/json')
|
||||||
expect(last_response).to be_ok
|
expect(last_response).to be_ok
|
||||||
response_json = JSON.parse(last_response.body)
|
response_json = JSON.parse(last_response.body)
|
||||||
expect(response_json['url']).to eq(data[:url])
|
expect(response_json['url']).to eq(data[:url])
|
||||||
expect(response_json['code']).not_to eq(nil)
|
expect(response_json['code']).not_to eq(nil)
|
||||||
expect(response_json['link']).to include(response_json['code'])
|
expect(response_json['link']).to include(response_json['code'])
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return with a 400 status and 'invalid url parameter' message when an empty url is submitted" do
|
it "should return with a 400 status and 'invalid url parameter' message when an empty url is submitted" do
|
||||||
data = {
|
data = {
|
||||||
url: ''
|
url: ''
|
||||||
}
|
}
|
||||||
post('/links', data.to_json, "CONTENT_TYPE" => "application/json")
|
post('/links', data.to_json, 'CONTENT_TYPE' => 'application/json')
|
||||||
expect(last_response.status).to eq(400)
|
expect(last_response.status).to eq(400)
|
||||||
response_json = JSON.parse(last_response.body)
|
response_json = JSON.parse(last_response.body)
|
||||||
expect(response_json['message']).to eq('invalid url parameter')
|
expect(response_json['message']).to eq('invalid url parameter')
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return with a 400 status and 'missing url parameter' message when an empty url is submitted" do
|
it "should return with a 400 status and 'missing url parameter' message when an empty url is submitted" do
|
||||||
data = {
|
data = {}
|
||||||
}
|
post('/links', data.to_json, 'CONTENT_TYPE' => 'application/json')
|
||||||
post('/links', data.to_json, "CONTENT_TYPE" => "application/json")
|
|
||||||
expect(last_response.status).to eq(400)
|
expect(last_response.status).to eq(400)
|
||||||
response_json = JSON.parse(last_response.body)
|
response_json = JSON.parse(last_response.body)
|
||||||
expect(response_json['message']).to eq('missing url parameter')
|
expect(response_json['message']).to eq('missing url parameter')
|
||||||
|
|
@ -63,7 +64,7 @@ describe "Submit API request to create new link" do
|
||||||
data = {
|
data = {
|
||||||
url: 'not-an-url'
|
url: 'not-an-url'
|
||||||
}
|
}
|
||||||
post('/links', data.to_json, "CONTENT_TYPE" => "application/json")
|
post('/links', data.to_json, 'CONTENT_TYPE' => 'application/json')
|
||||||
expect(last_response.status).to eq(400)
|
expect(last_response.status).to eq(400)
|
||||||
response_json = JSON.parse(last_response.body)
|
response_json = JSON.parse(last_response.body)
|
||||||
expect(response_json['message']).to eq('invalid url parameter')
|
expect(response_json['message']).to eq('invalid url parameter')
|
||||||
|
|
@ -73,7 +74,7 @@ describe "Submit API request to create new link" do
|
||||||
data = {
|
data = {
|
||||||
url: 'http://google.com/example'
|
url: 'http://google.com/example'
|
||||||
}
|
}
|
||||||
post('/links', data.to_json, "CONTENT_TYPE" => "application/json")
|
post('/links', data.to_json, 'CONTENT_TYPE' => 'application/json')
|
||||||
expect(last_response.status).to eq(400)
|
expect(last_response.status).to eq(400)
|
||||||
response_json = JSON.parse(last_response.body)
|
response_json = JSON.parse(last_response.body)
|
||||||
expect(response_json['message']).to eq('url not found')
|
expect(response_json['message']).to eq('url not found')
|
||||||
|
|
@ -83,13 +84,14 @@ describe "Submit API request to create new link" do
|
||||||
data = {
|
data = {
|
||||||
url: 'http://bad.tld'
|
url: 'http://bad.tld'
|
||||||
}
|
}
|
||||||
post('/links', data.to_json, "CONTENT_TYPE" => "application/json")
|
post('/links', data.to_json, 'CONTENT_TYPE' => 'application/json')
|
||||||
expect(last_response.status).to eq(400)
|
expect(last_response.status).to eq(400)
|
||||||
response_json = JSON.parse(last_response.body)
|
response_json = JSON.parse(last_response.body)
|
||||||
expect(response_json['message']).to eq('url does not resolve')
|
expect(response_json['message']).to eq('url does not resolve')
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return with a 400 status and 'not a valid json request' message when a request is made with the wrong content type header" do
|
it "should return with a 400 status and 'not a valid json request'
|
||||||
|
message when a request is made with the wrong content type header" do
|
||||||
data = {
|
data = {
|
||||||
url: 'http://google.com'
|
url: 'http://google.com'
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue