From 6dc1f65b1e25e82e8c6b111741a62a888a8c0c15 Mon Sep 17 00:00:00 2001 From: citin Date: Tue, 3 Sep 2024 16:40:00 +0200 Subject: [PATCH 1/3] adds services from authentication --- .../services/authentication/email_address.rb | 36 ++++++++ .../services/authentication/signer.rb | 2 +- .../authentication/user_and_password.rb | 92 +++++++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 lib/beyond_api/services/authentication/email_address.rb create mode 100644 lib/beyond_api/services/authentication/user_and_password.rb diff --git a/lib/beyond_api/services/authentication/email_address.rb b/lib/beyond_api/services/authentication/email_address.rb new file mode 100644 index 0000000..d55d6e2 --- /dev/null +++ b/lib/beyond_api/services/authentication/email_address.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module BeyondApi + module Authentication + # @example How to instantiate a client + # @client = BeyondApi::Authentication::EmailAddress.new( + # api_url: 'https://example.com/api', + # access_token: 'your_token' + # ) + class EmailAddress < BaseService + # Trigger an email address change + # + # @see https://developer.epages.com/beyond-docs/#trigger_email_address_change + # + # @option params [Integer] :user_id + # @option params [String] :locale defines the language of the confirmation email is to be sent. + # @option params [String] :current_password the current password of the user account. + # @option params [String] :new_email the new email address for the user to set. + # + # @example + # @client.trigger_change( + # "9ed8418f-d568-4327-9f20-ec1d00614398", + # "en-US", + # "GoodPassword01!;)", + # "new.email@epages.com" + # ) + def trigger_change(user_id, locale, current_password, new_email) + post( + "users/#{user_id}/change-email-request", + { current_password:, new_email: }, # body + locale # query params + ) + end + end + end +end diff --git a/lib/beyond_api/services/authentication/signer.rb b/lib/beyond_api/services/authentication/signer.rb index ec3704c..21d00eb 100644 --- a/lib/beyond_api/services/authentication/signer.rb +++ b/lib/beyond_api/services/authentication/signer.rb @@ -10,7 +10,7 @@ class Signer < BaseService # @see https://developer.epages.com/beyond-docs/#list_signers # # @return [Hash] - # # + # # @example # @client.all def all diff --git a/lib/beyond_api/services/authentication/user_and_password.rb b/lib/beyond_api/services/authentication/user_and_password.rb new file mode 100644 index 0000000..46e141f --- /dev/null +++ b/lib/beyond_api/services/authentication/user_and_password.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +module BeyondApi + module Authentication + # @example How to instantiate a client + # @client = BeyondApi::Authentication::UserAndPassword.new( + # api_url: 'https://example.com/api', + # access_token: 'your_token' + # ) + class UserAndPassword < BaseService + # Verify a password against the password guidelines. + # + # @see https://developer.epages.com/beyond-docs/#verify_password + # + # @option params [Integer] :user_role the type of user. Can be one of merchant, support, or customer. + # @option params [String] :password the password that needs to be verified. + # + # @example + # @client.verify_password( + # "merchant", + # "ValidPassword!" + # ) + def verify_password(user_role, password) + post( + 'users/verify-password', + { password: }, # body + user_role # query params + ) + end + + # Trigger an email address change + # + # @see https://developer.epages.com/beyond-docs/#change_password + # + # @option params [Integer] :user_id + # @option params [String] :current_password the current password of the user. This is verified before the password change. + # @option params [String] :new_password the new password to set in order to change the password. + # + # @example + # @client.change_password( + # "9ed8418f-d568-4327-9f20-ec1d00614398", + # "GoodPassword01!;)", + # "ValidPassword123" + # ) + def change_password(user_id, current_password, new_password) + post( + "users/#{user_id}/change-password", + { current_password:, new_password: } # body + ) + end + + # Trigger an email address change + # + # @see https://developer.epages.com/beyond-docs/#trigger_password_reset_email + # + # @option params [String] :email the email address of the user account. + # + # @example + # @client.password_reset_email( + # "baxter@example.org" + # ) + def password_reset_email(email) + post( + 'users/reset-password-request', + { email: } # body + ) + end + + # Change the username of a user. + # + # @see https://developer.epages.com/beyond-docs/#change_username + # + # @option params [Integer] :user_id the email address of the user account. + # @option params [String] :current_password The current password of the user. This is verified before the username change. + # @option params [String] :new_username The new username to set in order to change the username. + # + # @example + # @client.change_username( + # "9ed8418f-d568-4327-9f20-ec1d00614398", + # "ValidPassword123", + # "baxter2@example.org" + # ) + def change_username(user_id, current_password, new_username) + post( + "users/#{user_id}/change-username", + { current_password:, new_username: }, # body + user_role # query params + ) + end + end + end +end From e488f0d82da4e2d41fbd107b109392f0463c382c Mon Sep 17 00:00:00 2001 From: citin Date: Wed, 4 Sep 2024 13:18:54 +0200 Subject: [PATCH 2/3] [WIP] add tests --- .env.test.template | 6 ++ .../authentication/email_address_spec.rb | 22 +++++++ .../authentication/user_and_password_spec.rb | 66 +++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 spec/beyond_api/services/authentication/email_address_spec.rb create mode 100644 spec/beyond_api/services/authentication/user_and_password_spec.rb diff --git a/.env.test.template b/.env.test.template index d7e1ed4..9cfd31b 100644 --- a/.env.test.template +++ b/.env.test.template @@ -1,4 +1,10 @@ API_URL="" + CLIENT_ID="" CLIENT_SECRET="" REFRESH_TOKEN="" + +COCKPIT_USER_ID="" +COCKPIT_USER_NAME="" +COCKPIT_USER_EMAIL="" +COCKPIT_USER_PASSWORD="" diff --git a/spec/beyond_api/services/authentication/email_address_spec.rb b/spec/beyond_api/services/authentication/email_address_spec.rb new file mode 100644 index 0000000..0453358 --- /dev/null +++ b/spec/beyond_api/services/authentication/email_address_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +RSpec.describe BeyondApi::Authentication::EmailAddress, vcr: true do + let(:client) { described_class.new(api_url: ENV.fetch('API_URL', nil), access_token: beyond_access_token) } + + let(:user_id) { ENV.fetch('COCKPIT_USER_ID', nil) } + let(:user_email) { ENV.fetch('COCKPIT_USER_EMAIL', nil) } + let(:user_password) { ENV.fetch('COCKPIT_USER_PASSWORD', nil) } + + let(:new_email) { 'team42-new@epages.com' } + + describe '#trigger_change' do + it 'change email address' do + # client.trigger_change(user_id, 'en-US', user_password, new_email) + end + + after do + # # Rollback email address change + # client.trigger_change(user_id, 'en-US', user_password, user_email) + end + end +end diff --git a/spec/beyond_api/services/authentication/user_and_password_spec.rb b/spec/beyond_api/services/authentication/user_and_password_spec.rb new file mode 100644 index 0000000..b3ffe90 --- /dev/null +++ b/spec/beyond_api/services/authentication/user_and_password_spec.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +RSpec.describe BeyondApi::Authentication::UserAndPassword, vcr: true do + let(:client) { described_class.new(api_url: ENV.fetch('API_URL', nil), access_token: beyond_access_token) } + + let(:user_id) { ENV.fetch('COCKPIT_USER_ID', nil) } + let(:user_name) { ENV.fetch('COCKPIT_USER_NAME', nil) } + let(:user_email) { ENV.fetch('COCKPIT_USER_EMAIL', nil) } + let(:user_password) { ENV.fetch('COCKPIT_USER_PASSWORD', nil) } + + describe '#verify_password' do + let(:new_password) { 'ValidPassword123' } + + it 'sends a post request with the correct parameters' do + # client.verify_password('merchant', new_password) + end + end + + describe '#change_password' do + let(:new_password) { 'ValidPassword123' } + + it 'sends a post request with the correct parameters' do + # client.change_password( + # user_id, + # user_password, + # new_password + # ) + end + + after do + # # Rollback the password change + # client.change_password( + # user_id, + # new_password, + # user_password + # ) + end + end + + describe '#password_reset_email' do + it 'sends a post request with the correct parameters' do + # client.password_reset_email(user_email) + end + end + + describe '#change_username' do + let(:new_name) { 'test-user' } + + it 'sends a post request with the correct parameters' do + # client.change_username( + # user_id, + # user_password, + # new_name + # ) + end + + after do + # # Rollback the username change + # client.change_username( + # user_id, + # user_password, + # user_name + # ) + end + end +end From 8c70b3fc8ecf5eaf84cc1b3ff1cb134316e7e05a Mon Sep 17 00:00:00 2001 From: citin Date: Fri, 6 Sep 2024 11:06:26 +0200 Subject: [PATCH 3/3] fix dotenv for testing --- Gemfile | 2 ++ Gemfile.lock | 2 +- beyond_api.gemspec | 1 - .../authentication/email_address_spec.rb | 5 ++- .../authentication/user_and_password_spec.rb | 36 +++++++++---------- spec/spec_helper.rb | 2 ++ 6 files changed, 23 insertions(+), 25 deletions(-) diff --git a/Gemfile b/Gemfile index 6eff208..cb3b7a1 100644 --- a/Gemfile +++ b/Gemfile @@ -17,3 +17,5 @@ group :test do gem 'vcr' gem 'webmock' end + +gem 'dotenv', groups: [:development, :test] diff --git a/Gemfile.lock b/Gemfile.lock index a06e63e..6ec50e4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -141,7 +141,7 @@ PLATFORMS DEPENDENCIES beyond_api! bundler (~> 2.0) - dotenv (~> 2.7) + dotenv factory_bot faker (~> 2.2) jwt diff --git a/beyond_api.gemspec b/beyond_api.gemspec index 8948629..2b8e4c0 100644 --- a/beyond_api.gemspec +++ b/beyond_api.gemspec @@ -21,7 +21,6 @@ Gem::Specification.new do |spec| spec.required_ruby_version = '>= 3.3' spec.add_development_dependency 'bundler', '~> 2.0' - spec.add_development_dependency 'dotenv', '~> 2.7' spec.add_development_dependency 'faker', '~> 2.2' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rspec', '~> 3.0' diff --git a/spec/beyond_api/services/authentication/email_address_spec.rb b/spec/beyond_api/services/authentication/email_address_spec.rb index 0453358..c213ac6 100644 --- a/spec/beyond_api/services/authentication/email_address_spec.rb +++ b/spec/beyond_api/services/authentication/email_address_spec.rb @@ -10,9 +10,8 @@ let(:new_email) { 'team42-new@epages.com' } describe '#trigger_change' do - it 'change email address' do - # client.trigger_change(user_id, 'en-US', user_password, new_email) - end + # client.trigger_change(user_id, 'en-US', user_password, new_email) + it 'change email address' after do # # Rollback email address change diff --git a/spec/beyond_api/services/authentication/user_and_password_spec.rb b/spec/beyond_api/services/authentication/user_and_password_spec.rb index b3ffe90..248315d 100644 --- a/spec/beyond_api/services/authentication/user_and_password_spec.rb +++ b/spec/beyond_api/services/authentication/user_and_password_spec.rb @@ -11,21 +11,19 @@ describe '#verify_password' do let(:new_password) { 'ValidPassword123' } - it 'sends a post request with the correct parameters' do - # client.verify_password('merchant', new_password) - end + # client.verify_password('merchant', new_password) + it 'sends a post request with the correct parameters' end describe '#change_password' do let(:new_password) { 'ValidPassword123' } - it 'sends a post request with the correct parameters' do - # client.change_password( - # user_id, - # user_password, - # new_password - # ) - end + # client.change_password( + # user_id, + # user_password, + # new_password + # ) + it 'sends a post request with the correct parameters' after do # # Rollback the password change @@ -38,21 +36,19 @@ end describe '#password_reset_email' do - it 'sends a post request with the correct parameters' do - # client.password_reset_email(user_email) - end + # client.password_reset_email(user_email) + it 'sends a post request with the correct parameters' end describe '#change_username' do let(:new_name) { 'test-user' } - it 'sends a post request with the correct parameters' do - # client.change_username( - # user_id, - # user_password, - # new_name - # ) - end + # client.change_username( + # user_id, + # user_password, + # new_name + # ) + it 'sends a post request with the correct parameters' after do # # Rollback the username change diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index aa49eab..6550c50 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,6 +5,8 @@ require 'beyond_api' require 'factory_bot' +Dotenv.load('.env.test') + RSpec.configure do |config| # Enable flags like --only-failures and --next-failure config.example_status_persistence_file_path = '.rspec_status'