Skip to content
This repository was archived by the owner on Oct 7, 2022. It is now read-only.

Commit 2367255

Browse files
author
Andrej Jančič
committed
mocp api with sinatra and simulate a 404
1 parent e2baebf commit 2367255

File tree

7 files changed

+99
-3
lines changed

7 files changed

+99
-3
lines changed

lib/creatubbles/base_collection.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ def initialize(connection)
1313
@connection = connection
1414
end
1515

16-
def find(id)
16+
def find(id,opts={nil_404:false})
1717
res = @connection.get("#{self.class.type_name}/#{id}")
1818
Creatubbles.instantiate_object_from_response(res, @connection)
19+
rescue => e
20+
if opts[:nil_404] && is_record_not_found_404?(e)
21+
return nil
22+
else
23+
raise e
24+
end
1925
end
2026

2127
def init_objects(response)
@@ -26,4 +32,15 @@ def handle_params(params_hash={}, allowed_params=['query','filter'])
2632
params_hash.stringify_keys!.slice!(allowed_params).to_param
2733
end
2834

35+
private
36+
37+
def is_record_not_found_404?(e)
38+
return false unless e.is_a?(OAuth2::Error)
39+
body = e&.response&.body
40+
return false unless body
41+
resp = JSON.parse(e.response.body) rescue nil
42+
return false unless resp
43+
resp['errors'] && resp['errors'].any? { |err| err['status'].to_i == 404 }
44+
end
45+
2946
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe Creatubbles::BaseCollection do
4+
5+
it "ignores deleted creation" do
6+
client = Creatubbles::Client.dummy
7+
8+
expect{client.test_objects.find('non-existing-object-id')}.
9+
to raise_error(OAuth2::Error)
10+
11+
expect{client.test_objects.find('non-existing-object-id', nil_404:true)}.
12+
not_to raise_error
13+
end
14+
end

spec/creatubbles/creation_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'helper'
1+
require 'spec_helper'
22

33
describe Creatubbles::Creation do
44
let(:client) { Creatubbles::Client.new }

spec/creatubbles/user_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'helper'
1+
require 'spec_helper'
22

33
describe Creatubbles::User do
44
let(:client) { Creatubbles::Client.new }

spec/helper.rb renamed to spec/spec_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
require 'oauth2'
22
require 'rspec'
33
require 'creatubbles'
4+
require 'sinatra/base'
5+
require 'webmock/rspec'
6+
require 'pry'
47

58
Dir['spec/support/**/*.rb'].each do |f|
69
require f.gsub('spec/', '').gsub('.rb', '')
710
end
811

12+
WebMock.disable_net_connect!(allow_localhost: true)
13+
914
RSpec.configure do |config|
15+
config.before(:each) do
16+
stub_request(:any, /api.creatubbles.com/).to_rack(Creatubbles::MockAPI)
17+
end
18+
1019
config.expect_with :rspec do |c|
1120
c.syntax = :expect
1221
end

spec/support/creatubbles_mock_api.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'spec_helper'
2+
3+
module Creatubbles
4+
class MockAPI < Sinatra::Base
5+
set :port, 3000
6+
7+
# put class variables here
8+
# you cannot use @var in Sinatra::Base
9+
configure do
10+
end
11+
12+
# return a preset access token
13+
post '/oauth/token' do
14+
headers "Content-Type" => 'application/json'
15+
{"access_token" => "a8c814aa3cee2d5cdca997a314dca206335c119e7121e954a7c6f17592caa33d", "token_type" => "bearer", "created_at" => 1479918313}.to_json
16+
end
17+
18+
post '/v2/oauth/token' do
19+
headers "Content-Type" => 'application/json'
20+
{"access_token" => "a8c814aa3cee2d5cdca997a314dca206335c119e7121e954a7c6f17592caa33d", "token_type" => "bearer", "created_at" => 1479918313}.to_json
21+
end
22+
23+
# get a non existing object
24+
get '/v2/test_objects/non-existing-object-id' do
25+
status 404
26+
headers "Content-Type" => 'application/json'
27+
"{\"errors\":[{\"status\":\"404\",\"code\":\"not_found\",\"source\":\"https://api.creatubbles.com/v2/objects/non-existing-creation-id\",\"title\":\"Not found\",\"detail\":\"Record not found\"}]}"
28+
end
29+
30+
end
31+
end
32+
33+
# To run the moch as a separate process:
34+
# Creatubbles::MockAPI.run!

spec/support/dummy_client.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Creatubbles
2+
class Client
3+
4+
def self.dummy_auth_details
5+
{
6+
client_id: "dummy_id",
7+
client_secret: "dummy_secret",
8+
api_url: "https://api.creatubbles.com/v2"
9+
}
10+
end
11+
12+
def self.dummy
13+
new(dummy_auth_details)
14+
end
15+
16+
Creatubbles::BaseCollection.define_type_name('test_objects')
17+
18+
def test_objects
19+
Creatubbles::BaseCollection.new(connection)
20+
end
21+
end
22+
end

0 commit comments

Comments
 (0)