104 lines
3.4 KiB
Ruby
104 lines
3.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'simplecov'
|
|
SimpleCov.start
|
|
|
|
require_relative '../.env'
|
|
ENV['DB_NAME'] = "test_#{ENV.fetch('DB_NAME', nil)}"
|
|
require_relative '../app'
|
|
require 'rubygems'
|
|
require 'roda'
|
|
require 'sequel'
|
|
require 'rspec'
|
|
require 'rack/test'
|
|
|
|
# DB initialization
|
|
Sequel.extension :migration
|
|
Sequel.sqlite("db/#{ENV.fetch('DB_NAME', nil)}") 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.fetch('DB_NAME', nil)}")[: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
|
|
|
|
it "should return with a 400 status and 'invalid url parameter' message when an invalid url is submitted" do
|
|
data = {
|
|
url: 'not-an-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 'url not found' message when a 404 url is submitted" do
|
|
data = {
|
|
url: 'http://google.com/example'
|
|
}
|
|
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('url not found')
|
|
end
|
|
|
|
it "should return with a 400 status and 'url not found' message when a URL with no DNS is submitted" do
|
|
data = {
|
|
url: 'http://bad.tld'
|
|
}
|
|
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('url does not resolve')
|
|
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
|
|
data = {
|
|
url: 'http://google.com'
|
|
}
|
|
post('/links', data.to_json)
|
|
expect(last_response.status).to eq(400)
|
|
response_json = JSON.parse(last_response.body)
|
|
expect(response_json['message']).to eq('not a valid json request')
|
|
end
|
|
end
|