-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathfever_api_spec.rb
243 lines (187 loc) · 7.79 KB
/
fever_api_spec.rb
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
require "spec_helper"
require "./fever_api"
describe FeverAPI do
include Rack::Test::Methods
def app
FeverAPI::Endpoint
end
let(:api_key) { "apisecretkey" }
let(:story_one) { StoryFactory.build }
let(:story_two) { StoryFactory.build }
let(:group) { GroupFactory.build }
let(:feed) { FeedFactory.build(group_id: group.id) }
let(:stories) { [story_one, story_two] }
let(:standard_answer) do
{ api_version: 3, auth: 1, last_refreshed_on_time: 123456789 }
end
let(:cannot_auth) do
{ api_version: 3, auth: 0 }
end
let(:headers) { { api_key: api_key } }
before do
user = double(api_key: api_key)
allow(User).to receive(:first) { user }
allow(Time).to receive(:now) { Time.at(123456789) }
end
def last_response_as_object
JSON.parse(last_response.body, symbolize_names: true)
end
describe "authentication" do
it "authenticates request with correct api_key" do
get "/", headers
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
end
it "does not authenticate request with incorrect api_key" do
get "/", api_key: "foo"
expect(last_response).to be_ok
expect(last_response_as_object).to include(cannot_auth)
end
it "does not authenticate request when api_key is not provided" do
get "/"
expect(last_response).to be_ok
expect(last_response_as_object).to include(cannot_auth)
end
end
describe "#get" do
def make_request(extra_headers = {})
get "/", headers.merge(extra_headers)
end
it "returns standard answer" do
make_request
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
end
it "returns groups and feeds by groups when 'groups' header is provided" do
allow(GroupRepository).to receive(:list).and_return([group])
allow(FeedRepository).to receive_message_chain(:in_group, :order).and_return([feed])
make_request(groups: nil)
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
expect(last_response_as_object).to include(
groups: [group.as_fever_json],
feeds_groups: [{ group_id: group.id, feed_ids: feed.id.to_s }]
)
end
it "returns feeds and feeds by groups when 'feeds' header is provided" do
allow(FeedRepository).to receive(:list).and_return([feed])
allow(FeedRepository).to receive_message_chain(:in_group, :order).and_return([feed])
make_request(feeds: nil)
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
expect(last_response_as_object).to include(
feeds: [feed.as_fever_json],
feeds_groups: [{ group_id: group.id, feed_ids: feed.id.to_s }]
)
end
it "returns favicons hash when 'favicons' header provided" do
make_request(favicons: nil)
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
expect(last_response_as_object).to include(
favicons: [
{
id: 0,
data: "image/gif;base64,R0lGODlhAQABAIAAAObm5gAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
}
]
)
end
it "returns stories when 'items' header is provided along with 'since_id'" do
expect(StoryRepository).to receive(:unread_since_id).with("5").and_return([story_one])
expect(StoryRepository).to receive(:unread).and_return([story_one, story_two])
make_request(items: nil, since_id: 5)
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
expect(last_response_as_object).to include(
items: [story_one.as_fever_json],
total_items: 2
)
end
it "returns stories when 'items' header is provided without 'since_id'" do
expect(StoryRepository).to receive(:unread).twice.and_return([story_one, story_two])
make_request(items: nil)
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
expect(last_response_as_object).to include(
items: [story_one.as_fever_json, story_two.as_fever_json],
total_items: 2
)
end
it "returns stories ids when 'items' header is provided along with 'with_ids'" do
expect(StoryRepository).to receive(:fetch_by_ids).twice.with(["5"]).and_return([story_one])
make_request(items: nil, with_ids: 5)
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
expect(last_response_as_object).to include(
items: [story_one.as_fever_json],
total_items: 1
)
end
it "returns links as empty array when 'links' header is provided" do
make_request(links: nil)
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
expect(last_response_as_object).to include(links: [])
end
it "returns unread items ids when 'unread_item_ids' header is provided" do
expect(StoryRepository).to receive(:unread).and_return([story_one, story_two])
make_request(unread_item_ids: nil)
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
expect(last_response_as_object).to include(
unread_item_ids: [story_one.id, story_two.id].join(",")
)
end
it "returns starred items when 'saved_item_ids' header is provided" do
expect(Story).to receive(:where).with(is_starred: true).and_return([story_one, story_two])
make_request(saved_item_ids: nil)
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
expect(last_response_as_object).to include(
saved_item_ids: [story_one.id, story_two.id].join(",")
)
end
end
describe "#post" do
def make_request(extra_headers = {})
post "/", headers.merge(extra_headers)
end
it "commands to mark story as read" do
expect(MarkAsRead).to receive(:new).with("10").and_return(double(mark_as_read: true))
make_request(mark: "item", as: "read", id: 10)
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
end
it "commands to mark story as unread" do
expect(MarkAsUnread).to receive(:new).with("10").and_return(double(mark_as_unread: true))
make_request(mark: "item", as: "unread", id: 10)
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
end
it "commands to save story" do
expect(MarkAsStarred).to receive(:new).with("10").and_return(double(mark_as_starred: true))
make_request(mark: "item", as: "saved", id: 10)
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
end
it "commands to unsave story" do
expect(MarkAsUnstarred).to receive(:new).with("10").and_return(double(mark_as_unstarred: true))
make_request(mark: "item", as: "unsaved", id: 10)
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
end
it "commands to mark group as read" do
expect(MarkGroupAsRead).to receive(:new).with("10", "1375080946").and_return(double(mark_group_as_read: true))
make_request(mark: "group", as: "read", id: 10, before: 1375080946)
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
end
it "commands to mark entire feed as read" do
expect(MarkFeedAsRead).to receive(:new).with("20", "1375080945").and_return(double(mark_feed_as_read: true))
make_request(mark: "feed", as: "read", id: 20, before: 1375080945)
expect(last_response).to be_ok
expect(last_response_as_object).to include(standard_answer)
end
end
end