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 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 end