Skip to content

Commit

Permalink
Confirm User API v2 endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Milewski committed Jun 1, 2020
1 parent f608c28 commit 2e5ea1b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Spree
module Api
module V2
module Storefront
class AccountConfirmationsController < ::Spree::Api::V2::BaseController

def show
user = Spree.user_class.confirm_by_token(params[:id])

if user.errors.empty?
render json: { data: { state: user.respond_to?(:state) ? user.state : '' } }, status: :ok
else
render json: { error: user.errors.full_messages.to_sentence }, status: :unprocessable_entity
end
end
end
end
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
namespace :v2 do
namespace :storefront do
resource :account, controller: :account, only: %i[show create update]
resources :account_confirmations, only: %i[show]
resources :passwords, controller: :passwords, only: %i[create update]
end
end
Expand Down
44 changes: 44 additions & 0 deletions spec/requests/spree/api/v2/storefront/account_confirmation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'spec_helper'

describe 'Storefront API v2 Account Confirmation spec', type: :request do
describe 'account_confirmations#show' do

before do
set_confirmable_option(true)
get "/api/v2/storefront/account_confirmations/#{confirmation_token}"
end

after(:each) { set_confirmable_option(false) }

context 'valid confirmation_token param' do
let(:user) { create(:user, confirmation_token: '12345') }
let(:confirmation_token) { user.confirmation_token }

it_behaves_like 'returns 200 HTTP status'

it 'returns user state' do
expect(JSON.parse(response.body)['data']['state']).to eq('')
end
end

context 'invalid confirmation_token param' do
let(:confirmation_token) { 'dummy_token' }

it 'return 422 status' do
expect(response.code).to eq('422')
end

it 'return JSON API payload of error' do
expect(JSON.parse(response.body)['error']).to eq("Confirmation token is invalid")
end
end

context 'blank confirmation_token param' do
let(:confirmation_token) { '' }

it 'return 301 status' do
expect(response.code).to eq('301')
end
end
end
end

0 comments on commit 2e5ea1b

Please sign in to comment.