forked from openfoodfoundation/openfoodnetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalog_items_spec.rb
141 lines (112 loc) · 3.63 KB
/
catalog_items_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
# frozen_string_literal: true
require_relative "../swagger_helper"
describe "CatalogItems", type: :request, swagger_doc: "dfc.yaml",
rswag_autodoc: true do
let(:user) { create(:oidc_user, id: 12_345) }
let(:enterprise) {
create(
:distributor_enterprise,
id: 10_000, owner: user, name: "Fred's Farm", description: "Beautiful",
address: build(:address, id: 40_000),
)
}
let(:product) {
create(
:base_product,
id: 90_000, supplier: enterprise, name: "Apple", description: "Red",
variants: [variant],
primary_taxon: non_local_vegetable
)
}
let(:non_local_vegetable) {
build(
:taxon,
name: "Non Local Vegetable",
dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#non-local-vegetable"
)
}
let(:variant) { build(:base_variant, id: 10_001, unit_value: 1, sku: "AR") }
before { login_as user }
path "/api/dfc/enterprises/{enterprise_id}/catalog_items" do
parameter name: :enterprise_id, in: :path, type: :string
get "List CatalogItems" do
produces "application/json"
response "404", "not found" do
context "without enterprises" do
let(:enterprise_id) { "default" }
run_test!
end
context "with unrelated enterprise" do
let(:enterprise_id) { create(:enterprise).id }
run_test!
end
end
response "200", "success" do
before { product }
context "with default enterprise id" do
let(:enterprise_id) { "default" }
run_test! do
expect(response.body).to include "Apple"
expect(response.body).to include "AR"
expect(response.body).to include "offers/10001"
end
end
context "with given enterprise id" do
let(:enterprise_id) { 10_000 }
run_test! do
expect(response.body).to include "Apple"
expect(response.body).to include "AR"
expect(response.body).to include "offers/10001"
end
end
end
response "401", "unauthorized" do
let(:enterprise_id) { "default" }
before { login_as nil }
run_test!
end
end
end
path "/api/dfc/enterprises/{enterprise_id}/catalog_items/{id}" do
parameter name: :enterprise_id, in: :path, type: :string
parameter name: :id, in: :path, type: :string
get "Show CatalogItem" do
produces "application/json"
before { product }
response "200", "success" do
let(:enterprise_id) { 10_000 }
let(:id) { 10_001 }
run_test! do
expect(response.body).to include "dfc-b:CatalogItem"
expect(response.body).to include "offers/10001"
end
end
response "404", "not found" do
let(:enterprise_id) { 10_000 }
let(:id) { create(:variant).id }
run_test!
end
end
put "Update CatalogItem" do
consumes "application/json"
parameter name: :catalog_item, in: :body, schema: {
example: ExampleJson.read("patch_catalog_item")
}
before { product }
response "204", "no content" do
let(:enterprise_id) { 10_000 }
let(:id) { 10_001 }
let(:catalog_item) do |example|
example.metadata[:operation][:parameters].first[:schema][:example]
end
it "updates a variant" do |example|
expect {
submit_request(example.metadata)
variant.reload
}.to change { variant.on_hand }.to(3)
.and change { variant.sku }.to("new-sku")
end
end
end
end
end