From 4120d07424576f9b86d363b60d8a0d3bcb467c13 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Fri, 2 Jun 2023 20:25:10 +0100 Subject: [PATCH 1/2] Code style changes to conform to ruby code conventions. This includes: - removing uneccessary return statements - removing usage of static class variables - adding .freeze to class constants - using the faraday authorisation plugin - identation - using attr_accessor instead of getter and setter methods. --- .rubocop.yml | 8 ++ Gemfile | 11 +- lib/nordigen-ruby.rb | 190 ++++++++++++-------------- lib/nordigen_ruby/api/account.rb | 99 +++++++------- lib/nordigen_ruby/api/agreements.rb | 85 ++++++------ lib/nordigen_ruby/api/institutions.rb | 34 ++--- lib/nordigen_ruby/api/requisitions.rb | 113 ++++++++------- lib/nordigen_ruby/version.rb | 2 +- nordigen.gemspec | 8 +- tests/test_account.rb | 93 +++++++------ tests/test_agreements.rb | 104 +++++++------- tests/test_client.rb | 75 +++++----- tests/test_institution.rb | 51 ++++--- tests/test_requisitions.rb | 95 +++++++------ 14 files changed, 466 insertions(+), 502 deletions(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..a7ce05f --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,8 @@ +inherit_gem: + gc_ruboconfig: rubocop.yml + +AllCops: + Exclude: + - 'example/**/*' + - 'main.rb' + - 'nordigen-ruby.gemspec' diff --git a/Gemfile b/Gemfile index ec70e37..19081c7 100644 --- a/Gemfile +++ b/Gemfile @@ -1,14 +1,15 @@ # frozen_string_literal: true -source "https://rubygems.org" +source 'https://rubygems.org' # Specify your gem's dependencies in nordigen.gemspec gemspec -gem "faraday", "~> 2.5" +gem 'faraday', '~> 2.5' group :development do - gem "rake", "~> 13.0" - gem "test-unit", "~> 3.5.3" - gem "dotenv", "~> 2.8.1" + gem 'dotenv', '~> 2.8.1' + gem 'gc_ruboconfig' + gem 'rake', '~> 13.0' + gem 'test-unit', '~> 3.5.3' end diff --git a/lib/nordigen-ruby.rb b/lib/nordigen-ruby.rb index 288c0fe..0fda5a6 100644 --- a/lib/nordigen-ruby.rb +++ b/lib/nordigen-ruby.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "faraday" require_relative "nordigen_ruby/api/institutions" @@ -6,114 +8,96 @@ require_relative "nordigen_ruby/api/account" module Nordigen - class NordigenClient - - BASE_URL = "https://ob.nordigen.com/api/v2/" + class NordigenClient + BASE_URL = "https://ob.nordigen.com/api/v2/" + + attr_reader :secret_id, :secret_key, :institution, :agreement, :requisition + attr_accessor :access_token + + def initialize(secret_id:, secret_key:) + @secret_id = secret_id + @secret_key = secret_key + @institution = InstitutionsApi.new(self) + @agreement = AgreementsApi.new(self) + @requisition = RequisitionsApi.new(self) + @access_token = nil + end - @@headers = { - "accept" => "application/json", - "Content-Type" => "application/json", - "User-Agent" => "Nordigen-Ruby-v2" + def request + # HTTP client request + @request ||= Faraday.new do |conn| + conn.url_prefix = BASE_URL + conn.headers = { + "accept" => "application/json", + "Content-Type" => "application/json", + "User-Agent" => "Nordigen-Ruby-v2", } + conn.request :authorization, "Bearer", access_token + conn.request :json + conn.response :json + end + end - attr_reader :secret_id, :secret_key, :institution, :agreement, :requisition - - def initialize(secret_id:, secret_key:) - @secret_id = secret_id - @secret_key = secret_key - @institution = InstitutionsApi.new(client=self) - @agreement = AgreementsApi.new(client=self) - @requisition = RequisitionsApi.new(client=self) - end - - def request - # HTTP client request - @request ||= Faraday.new do |conn| - conn.url_prefix = BASE_URL - conn.headers = @@headers - conn.request :json - conn.response :json - end - end - - def set_token(access_token) - # Use existing token - @@headers["Authorization"] = "Bearer #{access_token}" - end - - def get_token - # Get token - return request.headers["Authorization"] - end - - def generate_token - # Generate new access & refresh token - payload = { - "secret_key": @secret_key, - "secret_id": @secret_id - } - response = self.request.post("token/new/", payload) - if !response.success? - raise Exception.new response.body - end - - @@headers["Authorization"] = "Bearer #{response.body['access']}" - request.headers = @@headers - return response.body - end - - def exchange_token(refresh_token) - # Exchange refresh token for access token - payload = {"refresh": refresh_token} - response = self.request.post("token/refresh/", payload).body - @@headers["Authorization"] = "Bearer #{response['access']}" - request.headers = @@headers - return response - end - - - def account(account_id) - # Create Account instance - return AccountApi.new(client: self, account_id: account_id) - end - - def init_session( - redirect_url:, - institution_id:, - reference_id:, - max_historical_days: 90, - access_valid_for_days: 90, - user_language: "en", - account_selection: false, - redirect_immediate: false, - ssn: nil - ) - # Factory method that creates authorization in a specific institution - # and are responsible for the following steps: - # * Creates agreement - # * Creates requisiton - - # Create agreement - new_agreement = @agreement.create_agreement( - institution_id: institution_id, - max_historical_days: max_historical_days, - access_valid_for_days: access_valid_for_days - ) + def generate_token + # Generate new access & refresh token + payload = { + secret_key: @secret_key, + secret_id: @secret_id, + } + response = request.post("token/new/", payload) + raise StandardError, response.body unless response.success? + + @access_token = response.body["access"] + response.body + end - # Create requisition - new_requsition = @requisition.create_requisition( - redirect_url: redirect_url, - reference: reference_id, - institution_id: institution_id, - user_language: user_language, - account_selection: account_selection, - redirect_immediate: redirect_immediate, - agreement: new_agreement["id"], - ssn: ssn - ) + def exchange_token(refresh_token) + # Exchange refresh token for access token + payload = { refresh: refresh_token } + response = request.post("token/refresh/", payload).body + @access_token = response["access"] + response + end - return new_requsition - end + def account(account_id) + # Create Account instance + AccountApi.new(client: self, account_id: account_id) + end + def init_session( + redirect_url:, + institution_id:, + reference_id:, + max_historical_days: 90, + access_valid_for_days: 90, + user_language: "en", + account_selection: false, + redirect_immediate: false, + ssn: nil + ) + # Factory method that creates authorization in a specific institution + # and are responsible for the following steps: + # * Creates agreement + # * Creates requisiton + + # Create agreement + new_agreement = @agreement.create_agreement( + institution_id: institution_id, + max_historical_days: max_historical_days, + access_valid_for_days: access_valid_for_days, + ) + + # Create requisition + @requisition.create_requisition( + redirect_url: redirect_url, + reference: reference_id, + institution_id: institution_id, + user_language: user_language, + account_selection: account_selection, + redirect_immediate: redirect_immediate, + agreement: new_agreement["id"], + ssn: ssn, + ) end + end end diff --git a/lib/nordigen_ruby/api/account.rb b/lib/nordigen_ruby/api/account.rb index fe3deb0..8e8206c 100644 --- a/lib/nordigen_ruby/api/account.rb +++ b/lib/nordigen_ruby/api/account.rb @@ -1,64 +1,61 @@ -module Nordigen - class AccountApi - - ENDPOINT = "accounts/" - PREMIUM_ENDPOINT = "accounts/premium/" - attr_reader :client, :account_id +# frozen_string_literal: true - def initialize(client:, account_id:) - @client = client - @account_id = account_id - end +module Nordigen + class AccountApi + ENDPOINT = "accounts/" + PREMIUM_ENDPOINT = "accounts/premium/" + attr_reader :client, :account_id + + def initialize(client:, account_id:) + @client = client + @account_id = account_id + end - def get(path = nil, params = nil, premium: nil) - # Create Get request - if premium - url = "#{PREMIUM_ENDPOINT}#{@account_id}/" + def get(path = nil, params = nil, premium: nil) + # Create Get request + url = if premium + "#{PREMIUM_ENDPOINT}#{@account_id}/" else - url = "#{ENDPOINT}#{@account_id}/" - end - - if path - url = "#{url}#{path}/" + "#{ENDPOINT}#{@account_id}/" end - return client.request.get(url, params).body - end + url = "#{url}#{path}/" if path - def get_metadata - # Access account metadata - return get() - end - - def get_details - # Access account details - return get("details") - end + client.request.get(url, params).body + end + def get_metadata + # Access account metadata + get + end - def get_balances - # Access account balances - return get("balances") - end + def get_details + # Access account details + get("details") + end - def get_transactions(date_from: nil, date_to: nil) - # Access account transactions - date_range = { - "date_from" => date_from, - "date_to" => date_to - } - return get("transactions", date_range) - end + def get_balances + # Access account balances + get("balances") + end - def get_premium_transactions(date_from: nil, date_to: nil, country: nil) - # Access account transactions - params = { - "date_from" => date_from, - "date_to" => date_to, - "country" => country - } - return get("transactions", params, premium: true ) - end + def get_transactions(date_from: nil, date_to: nil) + # Access account transactions + date_range = { + "date_from" => date_from, + "date_to" => date_to, + } + get("transactions", date_range) + end + def get_premium_transactions(date_from: nil, date_to: nil, country: nil) + # Access account transactions + params = { + "date_from" => date_from, + "date_to" => date_to, + "country" => country, + } + get("transactions", params, premium: true) end + end end diff --git a/lib/nordigen_ruby/api/agreements.rb b/lib/nordigen_ruby/api/agreements.rb index e0567ac..6f5a4a6 100644 --- a/lib/nordigen_ruby/api/agreements.rb +++ b/lib/nordigen_ruby/api/agreements.rb @@ -1,54 +1,49 @@ -module Nordigen - class AgreementsApi - - ENDPOINT = "agreements/enduser/" - attr_reader :client - - def initialize(client) - # Nordigen client initialization - @client = client - end +# frozen_string_literal: true - def create_agreement(institution_id:, max_historical_days: 90, access_valid_for_days: 90, access_scope: [ - "balances", - "details", - "transactions" - ]) - # Create enduser agreement - payload = { - "institution_id": institution_id, - "max_historical_days": max_historical_days, - "access_valid_for_days": access_valid_for_days, - "access_scope": access_scope, - }; - return client.request.post(ENDPOINT, payload).body - end +module Nordigen + class AgreementsApi + ENDPOINT = "agreements/enduser/" + attr_reader :client + def initialize(client) + # Nordigen client initialization + @client = client + end - def get_agreements(limit: 100, offset: 0) - # Get list of agreements - params = {limit: limit, offset: offset} - return client.request.get(ENDPOINT, params).body - end + def create_agreement(institution_id:, max_historical_days: 90, access_valid_for_days: 90, access_scope: %w[ + balances + details + transactions + ]) + # Create enduser agreement + payload = { + institution_id: institution_id, + max_historical_days: max_historical_days, + access_valid_for_days: access_valid_for_days, + access_scope: access_scope, + } + client.request.post(ENDPOINT, payload).body + end - def get_agreement_by_id(agreement_id) - # Get agreemenet by id - return client.request.get("#{ENDPOINT}#{agreement_id}/").body - end + def get_agreements(limit: 100, offset: 0) + # Get list of agreements + params = { limit: limit, offset: offset } + client.request.get(ENDPOINT, params).body + end - def delete_agreement(agreement_id) - # Delete agreement by id - return client.request.delete("#{ENDPOINT}#{agreement_id}/").body - end + def get_agreement_by_id(agreement_id) + # Get agreemenet by id + client.request.get("#{ENDPOINT}#{agreement_id}/").body + end - def accept_agreement(agreement_id:, ip:, user_agent:) - # Accept end user agreement - payload = { - 'user_agent': user_agent, - 'ip_address': ip - } - return client.request.put("#{ENDPOINT}#{agreement_id}/accept/").body - end + def delete_agreement(agreement_id) + # Delete agreement by id + client.request.delete("#{ENDPOINT}#{agreement_id}/").body + end + def accept_agreement(agreement_id:, ip:, user_agent:) + # Accept end user agreement + client.request.put("#{ENDPOINT}#{agreement_id}/accept/").body end + end end diff --git a/lib/nordigen_ruby/api/institutions.rb b/lib/nordigen_ruby/api/institutions.rb index c422d9b..8cad4b6 100644 --- a/lib/nordigen_ruby/api/institutions.rb +++ b/lib/nordigen_ruby/api/institutions.rb @@ -1,23 +1,23 @@ -module Nordigen - class InstitutionsApi - - ENDPOINT = "institutions" - attr_reader :client +# frozen_string_literal: true - def initialize(client) - # Nordigen client initialization - @client = client - end +module Nordigen + class InstitutionsApi + ENDPOINT = "institutions" + attr_reader :client - def get_institutions(country) - # Get list of institutions - return client.request.get("#{ENDPOINT}/?country=#{country}").body - end + def initialize(client) + # Nordigen client initialization + @client = client + end - def get_institution_by_id(id) - # Get single institution by id - return client.request.get("#{ENDPOINT}/#{id}/").body - end + def get_institutions(country) + # Get list of institutions + client.request.get("#{ENDPOINT}/?country=#{country}").body + end + def get_institution_by_id(id) + # Get single institution by id + client.request.get("#{ENDPOINT}/#{id}/").body end + end end diff --git a/lib/nordigen_ruby/api/requisitions.rb b/lib/nordigen_ruby/api/requisitions.rb index f0ec501..fd2efee 100644 --- a/lib/nordigen_ruby/api/requisitions.rb +++ b/lib/nordigen_ruby/api/requisitions.rb @@ -1,63 +1,58 @@ +# frozen_string_literal: true + module Nordigen - class RequisitionsApi - - ENDPOINT = "requisitions/" - attr_reader :client - - def initialize(client) - # Nordigen client initialization - @client = client - end - - def create_requisition( - redirect_url:, - reference:, - institution_id:, - user_language: "en", - agreement: nil, - account_selection: false, - redirect_immediate: false, - ssn: nil - ) - # Create requisition. For creating links and retrieving accounts. - # puts account_selection - # puts redirect_immediate - payload = { - "redirect": redirect_url, - "reference": reference, - "institution_id": institution_id, - "user_language": user_language, - "account_selection": account_selection, - "redirect_immediate": redirect_immediate, - } - - if agreement - payload["agreement"] = agreement - end - - if ssn - payload["ssn"] = ssn - end - - return client.request.post(ENDPOINT, payload).body - end - - def get_requisitions(limit: 100, offset: 0) - # Get all requisitions - params = {limit: limit, offset: offset} - return client.request.get(ENDPOINT, params).body - end - - - def get_requisition_by_id(requisition_id) - # Get requisition by id - return client.request.get("#{ENDPOINT}#{requisition_id}/").body - end - - def delete_requisition(requisition_id) - # Delete requisition by id - return client.request.delete("#{ENDPOINT}#{requisition_id}/").body - end + class RequisitionsApi + ENDPOINT = "requisitions/" + attr_reader :client + + def initialize(client) + # Nordigen client initialization + @client = client + end + + def create_requisition( + redirect_url:, + reference:, + institution_id:, + user_language: "en", + agreement: nil, + account_selection: false, + redirect_immediate: false, + ssn: nil + ) + # Create requisition. For creating links and retrieving accounts. + # puts account_selection + # puts redirect_immediate + payload = { + redirect: redirect_url, + reference: reference, + institution_id: institution_id, + user_language: user_language, + account_selection: account_selection, + redirect_immediate: redirect_immediate, + } + + payload["agreement"] = agreement if agreement + + payload["ssn"] = ssn if ssn + + client.request.post(ENDPOINT, payload).body + end + + def get_requisitions(limit: 100, offset: 0) + # Get all requisitions + params = { limit: limit, offset: offset } + client.request.get(ENDPOINT, params).body + end + + def get_requisition_by_id(requisition_id) + # Get requisition by id + client.request.get("#{ENDPOINT}#{requisition_id}/").body + end + def delete_requisition(requisition_id) + # Delete requisition by id + client.request.delete("#{ENDPOINT}#{requisition_id}/").body end + end end diff --git a/lib/nordigen_ruby/version.rb b/lib/nordigen_ruby/version.rb index 855f9ec..6b41947 100644 --- a/lib/nordigen_ruby/version.rb +++ b/lib/nordigen_ruby/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Nordigen - VERSION = "2.1.2" + VERSION = "2.1.2" end diff --git a/nordigen.gemspec b/nordigen.gemspec index e1700d4..0cd8907 100644 --- a/nordigen.gemspec +++ b/nordigen.gemspec @@ -27,14 +27,14 @@ Gem::Specification.new do |spec| /\A\.git/, /\Atest/, /\Aexample/, - /\Amain.rb/ + /\Amain.rb/, ) spec.files = `git ls-files`.split("\n").reject { |f| ignored.match(f) } - spec.executables = `git ls-files -- bin/*`.split("\n") - .map { |f| ::File.basename(f) } + spec.executables = `git ls-files -- bin/*`.split("\n"). + map { |f| ::File.basename(f) } spec.add_dependency "faraday", "~> 2.5" spec.require_paths = ["lib"] - + spec.metadata["rubygems_mfa_required"] = "true" end diff --git a/tests/test_account.rb b/tests/test_account.rb index f6c835d..4e1964c 100644 --- a/tests/test_account.rb +++ b/tests/test_account.rb @@ -1,54 +1,53 @@ -require 'test/unit' -require 'dotenv/load' +# frozen_string_literal: true -require_relative '../lib/nordigen_ruby/api/account' -require_relative '../lib/nordigen-ruby' +require "test/unit" +require "dotenv/load" + +require_relative "../lib/nordigen_ruby/api/account" +require_relative "../lib/nordigen-ruby" module Nordigen + class TestAccount < Test::Unit::TestCase + def setup + # Would it be possible to add a sandbox account + # to the SANDBOXFINANCE_SFIN0000 institution, + # that is always authorized? + omit("Not yet possible to test in CI") + + client = NordigenClient.new(secret_id: ENV["SECRET_ID"], + secret_key: ENV["SECRET_KEY"]) + client.generate_token + @account = AccountApi.new(client: client, account_id: ENV["ACCOUNT_ID"]) + @institution_id = "REVOLUT_REVOGB21" + end - class TestAccount < Test::Unit::TestCase - - def setup() - # Would it be possible to add a sandbox account - # to the SANDBOXFINANCE_SFIN0000 institution, - # that is always authorized? - omit('Not yet possible to test in CI') - - client = NordigenClient.new(secret_id: ENV["SECRET_ID"], secret_key: ENV["SECRET_KEY"]) - client.generate_token() - @account = AccountApi.new(client: client, account_id: ENV["ACCOUNT_ID"]) - @institution_id = "REVOLUT_REVOGB21" - end - - def test_account_metadata - # Test get account metadata - response = @account.get_metadata() - assert_equal(response["institution_id"], @institution_id) - end - - - def test_account_details - # Test get account details - response = @account.get_details() - assert_equal(response["account"]["currency"], "EUR") - end - - def test_account_balances - # Test get account balances - response = @account.get_balances() - currency = response["balances"][0]["balanceAmount"]["currency"] - assert_equal(currency, "EUR") - end - - def test_account_transactions - # Test get account transactions - response = @account.get_transactions() - assert_equal(response["booked"], nil) - end - - def test_account_transactions_with_date_range - omit('Need a test account with enough transactions to filter by date to test this') - end + def test_account_metadata + # Test get account metadata + response = @account.get_metadata + assert_equal(response["institution_id"], @institution_id) + end + + def test_account_details + # Test get account details + response = @account.get_details + assert_equal(response["account"]["currency"], "EUR") + end + + def test_account_balances + # Test get account balances + response = @account.get_balances + currency = response["balances"][0]["balanceAmount"]["currency"] + assert_equal(currency, "EUR") + end + + def test_account_transactions + # Test get account transactions + response = @account.get_transactions + assert_equal(response["booked"], nil) + end + def test_account_transactions_with_date_range + omit("Need a test account with enough transactions to filter by date to test this") end + end end diff --git a/tests/test_agreements.rb b/tests/test_agreements.rb index bcbb4bb..c65cb98 100644 --- a/tests/test_agreements.rb +++ b/tests/test_agreements.rb @@ -1,62 +1,60 @@ -require 'test/unit' -require 'dotenv/load' +# frozen_string_literal: true -require_relative '../lib/nordigen-ruby' +require "test/unit" +require "dotenv/load" + +require_relative "../lib/nordigen-ruby" require_relative "../lib/nordigen_ruby/api/agreements" module Nordigen + class TestAgreements < Test::Unit::TestCase + def setup + client = NordigenClient.new(secret_id: ENV["SECRET_ID"], + secret_key: ENV["SECRET_KEY"]) + client.generate_token + @agreement = AgreementsApi.new(client) + @institution_id = "REVOLUT_REVOGB21" + end + + def test_create_agreement + # Test create agreement + response = @agreement.create_agreement(institution_id: @institution_id) + assert_equal(response["institution_id"], @institution_id) + end - class TestAgreements < Test::Unit::TestCase - - def setup() - client = NordigenClient.new(secret_id: ENV["SECRET_ID"], secret_key: ENV["SECRET_KEY"]) - client.generate_token() - @agreement = AgreementsApi.new(client=client) - @institution_id = "REVOLUT_REVOGB21" - end - - def test_create_agreement - # Test create agreement - response = @agreement.create_agreement(institution_id: @institution_id) - assert_equal(response["institution_id"], @institution_id) - end - - - def test_get_agreement_by_id - # Test Get agreement by id - create_agreement = @agreement.create_agreement(institution_id: @institution_id) - agreement_id = create_agreement["id"] - response = @agreement.get_agreement_by_id(agreement_id) - assert_equal(response["id"], agreement_id) - end - - def test_get_agreements - # Test Get list of agreements - response = @agreement.get_agreements() - assert_equal(response["previous"], nil) - end - - def test_delete_agreement - # Test delete agreement - create_agreement = @agreement.create_agreement(institution_id: @institution_id) - agreement_id = create_agreement["id"] - response = @agreement.delete_agreement(agreement_id) - assert_equal(response["summary"], "End User Agreement deleted") - end - - def test_accept_agreement - # Test accept agreement - omit('Available only for premium users') - create_agreement = @agreement.create_agreement(institution_id: @institution_id) - agreement_id = create_agreement["id"] - response = @agreement.accept_agreement( - user_agent: "Chrome", - ip: "127.0.0.1", - agreement_id: agreement_id - ) - assert_equal(response["id"], agreement_id) - end + def test_get_agreement_by_id + # Test Get agreement by id + create_agreement = @agreement.create_agreement(institution_id: @institution_id) + agreement_id = create_agreement["id"] + response = @agreement.get_agreement_by_id(agreement_id) + assert_equal(response["id"], agreement_id) + end + + def test_get_agreements + # Test Get list of agreements + response = @agreement.get_agreements + assert_equal(response["previous"], nil) + end + def test_delete_agreement + # Test delete agreement + create_agreement = @agreement.create_agreement(institution_id: @institution_id) + agreement_id = create_agreement["id"] + response = @agreement.delete_agreement(agreement_id) + assert_equal(response["summary"], "End User Agreement deleted") end + def test_accept_agreement + # Test accept agreement + omit("Available only for premium users") + create_agreement = @agreement.create_agreement(institution_id: @institution_id) + agreement_id = create_agreement["id"] + response = @agreement.accept_agreement( + user_agent: "Chrome", + ip: "127.0.0.1", + agreement_id: agreement_id, + ) + assert_equal(response["id"], agreement_id) + end + end end diff --git a/tests/test_client.rb b/tests/test_client.rb index fc46135..f112e20 100644 --- a/tests/test_client.rb +++ b/tests/test_client.rb @@ -1,51 +1,40 @@ -require 'test/unit' -require 'dotenv/load' -require 'securerandom' +# frozen_string_literal: true -require_relative '../lib/nordigen-ruby' -require_relative '../lib/nordigen_ruby/api/institutions' - -module Nordigen - - class TestClient < Test::Unit::TestCase - - def setup() - @client = NordigenClient.new(secret_id: ENV["SECRET_ID"], secret_key: ENV["SECRET_KEY"]) - end - - def test_set_get_token - # Test setter and getter for token - new_token = "New token value" - @client.set_token(new_token) - token = @client.get_token() - assert_equal(token, "Bearer #{new_token}") - end - - def test_generate_token - # Test generate new token - response = @client.generate_token() - assert_equal(response["access_expires"], 86400) - end +require "test/unit" +require "dotenv/load" +require "securerandom" +require_relative "../lib/nordigen-ruby" +require_relative "../lib/nordigen_ruby/api/institutions" - def test_exchange_token - # Test exchange token - refresh_token = @client.generate_token()["refresh"] - response = @client.exchange_token(refresh_token) - assert_equal(response["access_expires"], 86400) - end - - def test_init_session - # Test initialize session - uuid = SecureRandom.uuid - id = "REVOLUT_REVOGB21" - response = @client.init_session( - redirect_url: "https://nordigen.com", institution_id: id, reference_id: uuid - ) - assert_equal(response["institution_id"], id) +module Nordigen + class TestClient < Test::Unit::TestCase + def setup + @client = NordigenClient.new(secret_id: ENV["SECRET_ID"], + secret_key: ENV["SECRET_KEY"]) + end - end + def test_generate_token + # Test generate new token + response = @client.generate_token + assert_equal(response["access_expires"], 86_400) + end + def test_exchange_token + # Test exchange token + refresh_token = @client.generate_token["refresh"] + response = @client.exchange_token(refresh_token) + assert_equal(response["access_expires"], 86_400) end + def test_init_session + # Test initialize session + uuid = SecureRandom.uuid + id = "REVOLUT_REVOGB21" + response = @client.init_session( + redirect_url: "https://nordigen.com", institution_id: id, reference_id: uuid, + ) + assert_equal(response["institution_id"], id) + end + end end diff --git a/tests/test_institution.rb b/tests/test_institution.rb index 7b098dc..1c6bd41 100644 --- a/tests/test_institution.rb +++ b/tests/test_institution.rb @@ -1,33 +1,32 @@ -require 'test/unit' -require 'dotenv/load' +# frozen_string_literal: true -require_relative '../lib/nordigen-ruby' -require_relative '../lib/nordigen_ruby/api/institutions' +require "test/unit" +require "dotenv/load" -module Nordigen - - class TestInstitutions < Test::Unit::TestCase - - def setup() - client = NordigenClient.new(secret_id: ENV["SECRET_ID"], secret_key: ENV["SECRET_KEY"]) - client.generate_token() - @institution = InstitutionsApi.new(client=client) - @institution_id = "REVOLUT_REVOGB21" - end - - def test_get_institutions - # Test get list of institutions - institutions = @institution.get_institutions("LV") - id = institutions.collect{ |k,v| k["id"]} - assert_includes(id, @institution_id) - end +require_relative "../lib/nordigen-ruby" +require_relative "../lib/nordigen_ruby/api/institutions" - def test_get_institution_by_id - # Test get institution by id - institutions = @institution.get_institution_by_id(id=@institution_id) - assert_equal(institutions["id"], @institution_id) - end +module Nordigen + class TestInstitutions < Test::Unit::TestCase + def setup + client = NordigenClient.new(secret_id: ENV["SECRET_ID"], + secret_key: ENV["SECRET_KEY"]) + client.generate_token + @institution = InstitutionsApi.new(client) + @institution_id = "REVOLUT_REVOGB21" + end + def test_get_institutions + # Test get list of institutions + institutions = @institution.get_institutions("LV") + id = institutions.collect { |k, _v| k["id"] } + assert_includes(id, @institution_id) end + def test_get_institution_by_id + # Test get institution by id + institutions = @institution.get_institution_by_id(@institution_id) + assert_equal(institutions["id"], @institution_id) + end + end end diff --git a/tests/test_requisitions.rb b/tests/test_requisitions.rb index 5d7344b..e22753c 100644 --- a/tests/test_requisitions.rb +++ b/tests/test_requisitions.rb @@ -1,56 +1,55 @@ -require 'test/unit' -require 'dotenv/load' -require 'securerandom' +# frozen_string_literal: true -require_relative '../lib/nordigen-ruby' -require_relative '../lib/nordigen_ruby/api/requisitions' +require "test/unit" +require "dotenv/load" +require "securerandom" + +require_relative "../lib/nordigen-ruby" +require_relative "../lib/nordigen_ruby/api/requisitions" module Nordigen + class TestRequisitions < Test::Unit::TestCase + def setup + client = NordigenClient.new(secret_id: ENV["SECRET_ID"], + secret_key: ENV["SECRET_KEY"]) + client.generate_token + @requisition = RequisitionsApi.new(client) + + @institution_id = "REVOLUT_REVOGB21" + @req_params = { + redirect_url: "https://ob.nordigen.com", + reference: SecureRandom.uuid, + institution_id: @institution_id, + } + end + + def test_create_requisition + # Test create requisition + SecureRandom.uuid + response = @requisition.create_requisition(**@req_params) + assert_equal(response["institution_id"], @institution_id) + end - class TestRequisitions < Test::Unit::TestCase - - def setup() - client = NordigenClient.new(secret_id: ENV["SECRET_ID"], secret_key: ENV["SECRET_KEY"]) - client.generate_token() - @requisition = RequisitionsApi.new(client=client) - - @institution_id = "REVOLUT_REVOGB21" - @req_params = { - redirect_url: "https://ob.nordigen.com", - reference: SecureRandom.uuid, - institution_id: @institution_id - } - end - - def test_create_requisition - # Test create requisition - uuid = SecureRandom.uuid - response = @requisition.create_requisition(**@req_params) - assert_equal(response["institution_id"], @institution_id) - end - - def test_get_requisitions - # Test get list of requisitions - response = @requisition.get_requisitions() - assert_equal(response["previous"], nil) - end - - def test_get_requisition_by_id - # Test get requisition by id - new_requisition = @requisition.create_requisition(**@req_params) - id = new_requisition["id"] - response = @requisition.get_requisition_by_id(id) - assert_equal(response["id"], id) - end - - def test_delete_requisition - # Test delete requisition - new_requisition = @requisition.create_requisition(**@req_params) - id = new_requisition["id"] - response = @requisition.delete_requisition(id) - assert_equal(response["summary"], "Requisition deleted") - end + def test_get_requisitions + # Test get list of requisitions + response = @requisition.get_requisitions + assert_equal(response["previous"], nil) + end + def test_get_requisition_by_id + # Test get requisition by id + new_requisition = @requisition.create_requisition(**@req_params) + id = new_requisition["id"] + response = @requisition.get_requisition_by_id(id) + assert_equal(response["id"], id) end + def test_delete_requisition + # Test delete requisition + new_requisition = @requisition.create_requisition(**@req_params) + id = new_requisition["id"] + response = @requisition.delete_requisition(id) + assert_equal(response["summary"], "Requisition deleted") + end + end end From e089691a46a49e1cd1829dc9be647a2555815010 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Fri, 2 Jun 2023 20:41:45 +0100 Subject: [PATCH 2/2] temp enable logger --- lib/nordigen-ruby.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/nordigen-ruby.rb b/lib/nordigen-ruby.rb index 0fda5a6..1153b1c 100644 --- a/lib/nordigen-ruby.rb +++ b/lib/nordigen-ruby.rb @@ -34,6 +34,7 @@ def request } conn.request :authorization, "Bearer", access_token conn.request :json + conn.response :logger, nil, { headers: true, bodies: true, errors: true } conn.response :json end end