-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv2_spec.rb
More file actions
129 lines (94 loc) · 3.37 KB
/
Copy pathv2_spec.rb
File metadata and controls
129 lines (94 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# frozen_string_literal: true
RSpec.describe IntelligentFoods::V2 do
describe "#base_url" do
context "when the environment is production" do
it "returns the correct url" do
api = IntelligentFoods::V2.new(environment: "production")
result = api.base_url
expect(result).to eq("https://core.intelligentfoods.com")
end
end
context "when the environment is not production" do
it "returns the correct url" do
api = IntelligentFoods::V2.new(environment: "staging")
result = api.base_url
expect(result).to eq("https://core.intelligentfoods.dev")
end
end
end
describe "#client" do
it "returns a api client" do
api = IntelligentFoods::V2.new
build_stubbed_authenticator(api)
result = api.client
expect(result).to respond_to(:post)
end
end
describe "#authenticate" do
it "builds a authenticator" do
api = IntelligentFoods::V2.new
build_stubbed_authenticator(api)
api.authenticate!
expect(IntelligentFoods::V2::Authenticator).to have_received(:build).once
end
it "authenticates" do
api = IntelligentFoods::V2.new
authenticator = build_stubbed_authenticator(api)
api.authenticate!
expect(authenticator).to have_received(:save!).once
end
context "when the authentication is present" do
it "is authenticated" do
api = IntelligentFoods::V2.new
build_stubbed_authenticator(api)
api.authenticate!
expect(api).to be_authenticated
end
end
end
describe "reset authentication" do
it "removes the present authentication" do
auth = IntelligentFoods::Authorization::Basic.new(token: "1234")
api = IntelligentFoods::V2.new(authentication: auth)
result = api.reset_authentication
expect(result.authentication).to be_blank
end
it "is not authenticated" do
auth = IntelligentFoods::Authorization::Basic.new(token: "1234")
api = IntelligentFoods::V2.new(authentication: auth)
result = api.reset_authentication
expect(result).not_to be_authenticated
end
end
describe ".build" do
it "sets the environment" do
config = build_config(environment: "preview")
result = IntelligentFoods::V2.build(config: config)
expect(result.environment).to eq("preview")
end
it "sets the username" do
config = build_config(username: "secretUsername")
result = IntelligentFoods::V2.build(config: config)
expect(result.username).to eq("secretUsername")
end
it "sets the password" do
config = build_config(password: "secretPassword")
result = IntelligentFoods::V2.build(config: config)
expect(result.password).to eq("secretPassword")
end
end
def build_config(environment: "staging", username: "string",
password: "string")
OpenStruct.new(environment: environment, username: username,
password: password)
end
def build_stubbed_authenticator(api)
authenticator = IntelligentFoods::V2::Authenticator.build(api)
authentication = authenticator.authentication
client = IntelligentFoods::ApiClient.new(authentication: authentication)
allow(IntelligentFoods::V2::Authenticator).to receive(:build).
and_return(authenticator)
allow(authenticator).to receive(:save!).and_return(client)
authenticator
end
end