summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMattia Roccoberton <mattiaroccoberton@nebulab.it>2021-02-12 12:37:03 +0100
committerAlessandro Desantis <desa.alessandro@gmail.com>2021-03-28 10:25:46 +0200
commit5f14a24c9e9c1a701077d3ac56a632583e51742b (patch)
tree374da57b7bb93d14f50fc5bf14f39d334533c46c
parent6cbd8887c4df74e4e3a010c34b706156b16e8211 (diff)
Add a subscription create endpoint
Mostly the same as update endpoint.
-rw-r--r--app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb12
-rw-r--r--config/routes.rb2
-rw-r--r--spec/requests/api/v1/subscriptions_spec.rb34
3 files changed, 47 insertions, 1 deletions
diff --git a/app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb b/app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb
index 4cd698e..0ff53ba 100644
--- a/app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb
+++ b/app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb
@@ -6,6 +6,18 @@ module SolidusSubscriptions
class SubscriptionsController < BaseController
protect_from_forgery unless: -> { request.format.json? }
+ def create
+ store = params[:store_id].nil? ? ::Spree::Store.default : ::Spree::Store.find(id: params[:store_id])
+ attributes = subscription_params.merge(user: current_api_user, store: store)
+ subscription = SolidusSubscriptions::Subscription.new(attributes)
+
+ if subscription.save
+ render json: subscription.to_json(include: [:line_items, :shipping_address, :billing_address])
+ else
+ render json: subscription.errors.to_json, status: :unprocessable_entity
+ end
+ end
+
def update
load_subscription
diff --git a/config/routes.rb b/config/routes.rb
index 04bad4c..cbed8c3 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -4,7 +4,7 @@ SolidusSubscriptions::Engine.routes.draw do
namespace :api, defaults: { format: :json } do
namespace :v1 do
resources :line_items, only: [:update, :destroy]
- resources :subscriptions, only: [:update] do
+ resources :subscriptions, only: [:create, :update] do
member do
post :cancel
post :skip
diff --git a/spec/requests/api/v1/subscriptions_spec.rb b/spec/requests/api/v1/subscriptions_spec.rb
index 6807c67..3031f14 100644
--- a/spec/requests/api/v1/subscriptions_spec.rb
+++ b/spec/requests/api/v1/subscriptions_spec.rb
@@ -3,6 +3,40 @@
RSpec.describe '/api/v1/subscriptions' do
include SolidusSubscriptions::Engine.routes.url_helpers
+ describe 'POST /' do
+ context 'with valid params' do
+ it 'creates the subscription and responds with 200 OK' do
+ user = create(:user, &:generate_spree_api_key!)
+
+ expect do
+ post(
+ api_v1_subscriptions_path,
+ params: { subscription: { interval_length: 11 } },
+ headers: { 'Authorization' => "Bearer #{user.spree_api_key}" },
+ )
+ end.to change(SolidusSubscriptions::Subscription, :count).from(0).to(1)
+
+ expect(response.status).to eq(200)
+ end
+ end
+
+ context 'with invalid params' do
+ it "doesn't create the subscription and responds with 422 Unprocessable Entity" do
+ user = create(:user, &:generate_spree_api_key!)
+
+ expect do
+ post(
+ api_v1_subscriptions_path,
+ params: { subscription: { interval_length: -1 } },
+ headers: { 'Authorization' => "Bearer #{user.spree_api_key}" },
+ )
+ end.not_to change(SolidusSubscriptions::Subscription, :count)
+
+ expect(response.status).to eq(422)
+ end
+ end
+ end
+
describe 'PATCH /:id' do
context 'when the subscription belongs to the user' do
context 'with valid params' do