-
Notifications
You must be signed in to change notification settings - Fork 18
added impersonate method #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aa93646
added impersonate
bartes 35997a4
spec styling
bartes aee5db3
added validation of user_agent ip and client for impersonate, remove …
bartes 5733283
corrected impersonate specs
bartes e01245d
spec stylings
bartes 1c59e0a
dont force to send client_id
bartes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,40 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Castle | ||
| module Commands | ||
| # builder for impersonate command | ||
| class Impersonate | ||
| def initialize(context) | ||
| @context = context | ||
| end | ||
|
|
||
| def build(options = {}) | ||
| validate!(options) | ||
| context = ContextMerger.call(@context, options[:context]) | ||
| context = ContextSanitizer.call(context) | ||
|
|
||
| validate_context!(context) | ||
|
|
||
| Castle::Command.new('impersonate', | ||
| options.merge(context: context), | ||
| :post) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def validate!(options) | ||
| %i[user_id].each do |key| | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From impersonate endpoint: Please update validation
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lluft - I'm changing the endpoint . It will look like this before merging this: |
||
| next unless options[key].to_s.empty? | ||
| raise Castle::InvalidParametersError, "#{key} is missing or empty" | ||
| end | ||
| end | ||
|
|
||
| def validate_context!(context) | ||
| %i[user_agent ip].each do |key| | ||
| next unless context[key].to_s.empty? | ||
| raise Castle::InvalidParametersError, "#{key} is missing or empty" | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,99 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| describe Castle::Commands::Impersonate do | ||
| subject(:instance) { described_class.new(context) } | ||
|
|
||
| let(:context) { { user_agent: 'test', ip: '127.0.0.1', client_id: 'test' } } | ||
| let(:impersonator) { 'test@castle.io' } | ||
| let(:default_payload) { { user_id: '1234' } } | ||
|
|
||
| let(:time_now) { Time.now } | ||
| let(:time_auto) { time_now.utc.iso8601(3) } | ||
|
|
||
| before { Timecop.freeze(time_now) } | ||
| after { Timecop.return } | ||
|
|
||
| describe '.build' do | ||
| subject(:command) { instance.build(payload) } | ||
|
|
||
| context 'with simple merger' do | ||
| let(:payload) { default_payload.merge(context: { test: { test2: '1' } }) } | ||
| let(:command_data) do | ||
| default_payload.merge( | ||
| context: { | ||
| test: { test2: '1' }, | ||
| user_agent: 'test', | ||
| ip: '127.0.0.1', | ||
| client_id: 'test' | ||
| } | ||
| ) | ||
| end | ||
|
|
||
| it { expect(command.method).to be_eql(:post) } | ||
| it { expect(command.path).to be_eql('impersonate') } | ||
| it { expect(command.data).to be_eql(command_data) } | ||
| end | ||
|
|
||
| context 'with impersonator' do | ||
| let(:payload) { default_payload.merge(impersonator: impersonator) } | ||
| let(:command_data) do | ||
| default_payload.merge(impersonator: impersonator, context: context) | ||
| end | ||
|
|
||
| it { expect(command.method).to be_eql(:post) } | ||
| it { expect(command.path).to be_eql('impersonate') } | ||
| it { expect(command.data).to be_eql(command_data) } | ||
| end | ||
|
|
||
| context 'when active true' do | ||
| let(:payload) { default_payload.merge(context: { active: true }) } | ||
| let(:command_data) do | ||
| default_payload.merge(context: context.merge(active: true)) | ||
| end | ||
|
|
||
| it { expect(command.method).to be_eql(:post) } | ||
| it { expect(command.path).to be_eql('impersonate') } | ||
| it { expect(command.data).to be_eql(command_data) } | ||
| end | ||
|
|
||
| context 'when active false' do | ||
| let(:payload) { default_payload.merge(context: { active: false }) } | ||
| let(:command_data) do | ||
| default_payload.merge(context: context.merge(active: false)) | ||
| end | ||
|
|
||
| it { expect(command.method).to be_eql(:post) } | ||
| it { expect(command.path).to be_eql('impersonate') } | ||
| it { expect(command.data).to be_eql(command_data) } | ||
| end | ||
|
|
||
| context 'when active string' do | ||
| let(:payload) { default_payload.merge(context: { active: 'string' }) } | ||
| let(:command_data) { default_payload.merge(context: context) } | ||
|
|
||
| it { expect(command.method).to be_eql(:post) } | ||
| it { expect(command.path).to be_eql('impersonate') } | ||
| it { expect(command.data).to be_eql(command_data) } | ||
| end | ||
| end | ||
|
|
||
| describe '#validate!' do | ||
| subject(:validate!) { instance.build(payload) } | ||
|
|
||
| context 'when user_id not present' do | ||
| let(:payload) { {} } | ||
|
|
||
| it do | ||
| expect do | ||
| validate! | ||
| end.to raise_error(Castle::InvalidParametersError, 'user_id is missing or empty') | ||
| end | ||
| end | ||
|
|
||
| context 'when user_id present' do | ||
| let(:payload) { { user_id: '1234' } } | ||
|
|
||
| it { expect { validate! }.not_to raise_error } | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this. Doesn't apply to impersonate