|
2 | 2 |
|
3 | 3 | require 'spec_helper'
|
4 | 4 |
|
5 |
| -module Spree |
6 |
| - module Api |
7 |
| - class WidgetsController < Spree::Api::ResourceController |
8 |
| - prepend_view_path('spec/test_views') |
| 5 | +describe Spree::Api::ResourceController, type: :controller do |
| 6 | + it "is deprecated" do |
| 7 | + expect(Spree::Deprecation).to receive(:warn).with(/deprecated/) |
9 | 8 |
|
10 |
| - def model_class |
11 |
| - Widget |
12 |
| - end |
13 |
| - |
14 |
| - def permitted_widget_attributes |
15 |
| - [:name] |
16 |
| - end |
17 |
| - end |
18 |
| - end |
19 |
| - |
20 |
| - describe Api::WidgetsController, type: :controller do |
21 |
| - render_views |
22 |
| - |
23 |
| - after(:all) do |
24 |
| - Rails.application.reload_routes! |
25 |
| - end |
26 |
| - |
27 |
| - with_model 'Widget', scope: :all do |
28 |
| - table do |widget| |
29 |
| - widget.string :name |
30 |
| - widget.integer :position |
31 |
| - widget.timestamps null: false |
32 |
| - end |
33 |
| - |
34 |
| - model do |
35 |
| - acts_as_list |
36 |
| - validates :name, presence: true |
37 |
| - end |
38 |
| - end |
39 |
| - |
40 |
| - before do |
41 |
| - Spree::Core::Engine.routes.draw do |
42 |
| - namespace :api do |
43 |
| - resources :widgets |
44 |
| - end |
45 |
| - end |
46 |
| - end |
47 |
| - |
48 |
| - let(:user) { create(:user, :with_api_key) } |
49 |
| - let(:admin_user) { create(:admin_user, :with_api_key) } |
50 |
| - |
51 |
| - describe "#index" do |
52 |
| - let!(:widget) { Widget.create!(name: "a widget") } |
53 |
| - |
54 |
| - it "returns no widgets" do |
55 |
| - get :index, params: { token: user.spree_api_key }, as: :json |
56 |
| - expect(response).to be_successful |
57 |
| - expect(json_response['widgets']).to be_blank |
58 |
| - end |
59 |
| - |
60 |
| - context "it has authorization to read widgets" do |
61 |
| - it "returns widgets" do |
62 |
| - get :index, params: { token: admin_user.spree_api_key }, as: :json |
63 |
| - expect(response).to be_successful |
64 |
| - expect(json_response['widgets']).to include( |
65 |
| - hash_including( |
66 |
| - 'name' => 'a widget', |
67 |
| - 'position' => 1 |
68 |
| - ) |
69 |
| - ) |
70 |
| - end |
71 |
| - |
72 |
| - context "specifying ids" do |
73 |
| - let!(:widget2) { Widget.create!(name: "a widget") } |
74 |
| - |
75 |
| - it "returns both widgets from comma separated string" do |
76 |
| - get :index, params: { ids: [widget.id, widget2.id].join(','), token: admin_user.spree_api_key }, as: :json |
77 |
| - expect(response).to be_successful |
78 |
| - expect(json_response['widgets'].size).to eq 2 |
79 |
| - end |
80 |
| - |
81 |
| - it "returns both widgets from multiple arguments" do |
82 |
| - get :index, params: { ids: [widget.id, widget2.id], token: admin_user.spree_api_key }, as: :json |
83 |
| - expect(response).to be_successful |
84 |
| - expect(json_response['widgets'].size).to eq 2 |
85 |
| - end |
86 |
| - |
87 |
| - it "returns one requested widgets" do |
88 |
| - get :index, params: { ids: widget2.id.to_s, token: admin_user.spree_api_key }, as: :json |
89 |
| - expect(response).to be_successful |
90 |
| - expect(json_response['widgets'].size).to eq 1 |
91 |
| - expect(json_response['widgets'][0]['id']).to eq widget2.id |
92 |
| - end |
93 |
| - |
94 |
| - it "returns no widgets if empty" do |
95 |
| - get :index, params: { ids: '', token: admin_user.spree_api_key }, as: :json |
96 |
| - expect(response).to be_successful |
97 |
| - expect(json_response['widgets']).to be_empty |
98 |
| - end |
99 |
| - end |
100 |
| - end |
101 |
| - end |
102 |
| - |
103 |
| - describe "#show" do |
104 |
| - let(:widget) { Widget.create!(name: "a widget") } |
105 |
| - |
106 |
| - it "returns not found" do |
107 |
| - get :show, params: { id: widget.to_param, token: user.spree_api_key }, as: :json |
108 |
| - assert_not_found! |
109 |
| - end |
110 |
| - |
111 |
| - context "it has authorization read widgets" do |
112 |
| - it "returns widget details" do |
113 |
| - get :show, params: { id: widget.to_param, token: admin_user.spree_api_key }, as: :json |
114 |
| - expect(response).to be_successful |
115 |
| - expect(json_response['name']).to eq 'a widget' |
116 |
| - end |
117 |
| - end |
118 |
| - end |
119 |
| - |
120 |
| - describe "#new" do |
121 |
| - it "returns unauthorized" do |
122 |
| - get :new, params: { token: user.spree_api_key }, as: :json |
123 |
| - expect(response).to be_unauthorized |
124 |
| - end |
125 |
| - |
126 |
| - context "it is allowed to view a new widget" do |
127 |
| - it "can learn how to create a new widget" do |
128 |
| - get :new, params: { token: admin_user.spree_api_key }, as: :json |
129 |
| - expect(response).to be_successful |
130 |
| - expect(json_response["attributes"]).to eq(['name']) |
131 |
| - end |
132 |
| - end |
133 |
| - end |
134 |
| - |
135 |
| - describe "#create" do |
136 |
| - it "returns unauthorized" do |
137 |
| - expect { |
138 |
| - post :create, params: { widget: { name: "a widget" }, token: user.spree_api_key }, as: :json |
139 |
| - }.not_to change(Widget, :count) |
140 |
| - expect(response).to be_unauthorized |
141 |
| - end |
142 |
| - |
143 |
| - context "it is authorized to create widgets" do |
144 |
| - it "can create a widget" do |
145 |
| - expect { |
146 |
| - post :create, params: { widget: { name: "a widget" }, token: admin_user.spree_api_key }, as: :json |
147 |
| - }.to change(Widget, :count).by(1) |
148 |
| - expect(response).to be_created |
149 |
| - expect(json_response['name']).to eq 'a widget' |
150 |
| - expect(Widget.last.name).to eq 'a widget' |
151 |
| - end |
152 |
| - end |
153 |
| - end |
154 |
| - |
155 |
| - describe "#update" do |
156 |
| - let!(:widget) { Widget.create!(name: "a widget") } |
157 |
| - it "returns unauthorized" do |
158 |
| - put :update, params: { id: widget.to_param, widget: { name: "another widget" }, token: user.spree_api_key }, as: :json |
159 |
| - assert_not_found! |
160 |
| - expect(widget.reload.name).to eq 'a widget' |
161 |
| - end |
162 |
| - |
163 |
| - context "it is authorized to update widgets" do |
164 |
| - it "can update a widget" do |
165 |
| - put :update, params: { id: widget.to_param, widget: { name: "another widget" }, token: admin_user.spree_api_key }, as: :json |
166 |
| - expect(response).to be_successful |
167 |
| - expect(json_response['name']).to eq 'another widget' |
168 |
| - expect(widget.reload.name).to eq 'another widget' |
169 |
| - end |
170 |
| - end |
171 |
| - end |
172 |
| - |
173 |
| - describe "#destroy" do |
174 |
| - let!(:widget) { Widget.create!(name: "a widget") } |
175 |
| - |
176 |
| - it "returns unauthorized" do |
177 |
| - delete :destroy, params: { id: widget.to_param, token: user.spree_api_key }, as: :json |
178 |
| - assert_not_found! |
179 |
| - expect { Widget.find(widget.id) }.not_to raise_error |
180 |
| - end |
181 |
| - |
182 |
| - context "it is authorized to destroy widgets" do |
183 |
| - it "hard-deletes the widget" do |
184 |
| - delete :destroy, params: { id: widget.to_param, token: admin_user.spree_api_key }, as: :json |
185 |
| - expect(response.status).to eq 204 |
186 |
| - expect { Widget.find(widget.id) }.to raise_error(ActiveRecord::RecordNotFound) |
187 |
| - end |
188 |
| - end |
189 |
| - end |
| 9 | + expect(Class.new(described_class)) |
190 | 10 | end
|
191 | 11 | end
|
0 commit comments