-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add instance method on Customer object to generate a login token for that customer. This only works if the app has the store_v2_customer_login scope and is installed on the store.
- Loading branch information
Showing
10 changed files
with
86 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ pkg/* | |
tmp/* | ||
.env | ||
.env-* | ||
vendor/bundle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'bigcommerce' | ||
|
||
Bigcommerce.configure do |config| | ||
config.store_hash = ENV['BC_STORE_HASH'] | ||
config.client_id = ENV['BC_CLIENT_ID'] | ||
config.client_secret = ENV['BC_CLIENT_SECRET'] | ||
config.access_token = ENV['BC_ACCESS_TOKEN'] | ||
end | ||
|
||
# Get a customer | ||
customer = Bigcommerce::Customer.all(page: 1).first | ||
|
||
# Generate token login url | ||
puts customer.login_token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 29 additions & 4 deletions
33
spec/bigcommerce/unit/resources/customers/customer_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,35 @@ | ||
RSpec.describe Bigcommerce::Customer do | ||
before(:each) { @customer = Bigcommerce::Customer } | ||
|
||
describe '.count' do | ||
it 'should hit the correct path' do | ||
expect(@customer).to receive(:get).with('customers/count', {}) | ||
@customer.count | ||
expect(described_class).to receive(:get).with('customers/count', {}) | ||
described_class.count | ||
end | ||
end | ||
|
||
describe '.login_token' do | ||
let(:client_id) { SecureRandom.hex(6) } | ||
let(:client_secret) { SecureRandom.hex(6) } | ||
let(:store_hash) { SecureRandom.hex(4) } | ||
let(:customer_id) { Random.rand(1000) } | ||
let(:customer) { described_class.new(id: customer_id) } | ||
subject { customer.login_token } | ||
|
||
before do | ||
Bigcommerce.configure do |config| | ||
config.store_hash = store_hash | ||
config.client_id = client_id | ||
config.client_secret = client_secret | ||
end | ||
end | ||
|
||
it 'should generate a signed token with the right payload' do | ||
payload = JWT.decode(subject, client_secret, true, { :algorithm => 'HS256' })[0] | ||
expect(payload['iss']).to eq(client_id) | ||
expect(payload['store_hash']).to eq(store_hash) | ||
expect(payload['operation']).to eq('customer_login') | ||
expect(payload['customer_id']).to eq(customer_id) | ||
expect(payload['iat']).to be <= Time.now.to_i | ||
expect(payload['jti']).to_not be_empty | ||
end | ||
end | ||
end |