diff --git a/lib/fabricio/authorization/authorization_client.rb b/lib/fabricio/authorization/authorization_client.rb index 4c61c5a..e34466b 100644 --- a/lib/fabricio/authorization/authorization_client.rb +++ b/lib/fabricio/authorization/authorization_client.rb @@ -4,7 +4,6 @@ require 'fabricio/services/organization_service' AUTH_API_URL = 'https://fabric.io/oauth/token' -ORGANIZATION_API_URL = 'https://fabric.io/api/v2/organizations' module Fabricio module Authorization @@ -46,8 +45,7 @@ def perform_authorization(username, password, client_id, client_secret) if auth_data['access_token'] == nil raise StandardError.new("Incorrect authorization response: #{auth_data}") end - organization_id = obtain_organization_id(auth_data) - Session.new(auth_data, organization_id) + Session.new(auth_data) end # Initiates a session refresh network request @@ -71,7 +69,7 @@ def perform_refresh_token_request(session) if result['access_token'] == nil raise StandardError.new("Incorrect authorization response: #{auth_data}") end - Session.new(result, session.organization_id) + Session.new(result) end # Makes a request to OAuth API and obtains access and refresh tokens. @@ -99,24 +97,6 @@ def obtain_auth_data(username, password, client_id, client_secret) end JSON.parse(response.body) end - - # Makes a request to fetch current organization identifier. - # This identifier is used in most other API requests, so we store it in the session. - # - # @param auth_data [Hash] A set of authorization tokens - # @option options [String] :access_token OAuth access token - # @option options [String] :refresh_token OAuth refresh token - # @return [String] - def obtain_organization_id(auth_data) - conn = Faraday.new(:url => ORGANIZATION_API_URL) do |faraday| - faraday.adapter Faraday.default_adapter - end - - response = conn.get do |req| - req.headers['Authorization'] = "Bearer #{auth_data['access_token']}" - end - JSON.parse(response.body).first['id'] - end end end end diff --git a/lib/fabricio/authorization/session.rb b/lib/fabricio/authorization/session.rb index 380ccf8..aa9b933 100644 --- a/lib/fabricio/authorization/session.rb +++ b/lib/fabricio/authorization/session.rb @@ -2,19 +2,17 @@ module Fabricio module Authorization # This class is a data structure that holds tokens and identifiers necessary for making API requests. class Session - attr_reader :access_token, :refresh_token, :organization_id + attr_reader :access_token, :refresh_token # Initializes a new Session object # # @param attributes [Hash] Hash containing access and refresh tokens # @option options [String] :access_token OAuth access token # @option options [String] :refresh_token OAuth refresh token - # @param organization_id [String] # @return [Fabricio::Authorization::Session] - def initialize(attributes = {}, organization_id = '') + def initialize(attributes = {}) @access_token = attributes['access_token'] @refresh_token = attributes['refresh_token'] - @organization_id = organization_id end end end diff --git a/lib/fabricio/client/client.rb b/lib/fabricio/client/client.rb index 1f7ac04..c346ee5 100644 --- a/lib/fabricio/client/client.rb +++ b/lib/fabricio/client/client.rb @@ -6,6 +6,7 @@ require 'fabricio/authorization/session' require 'fabricio/authorization/memory_session_storage' require 'fabricio/networking/network_client' +require 'fabricio/networking/organization_id_provider' module Fabricio # The main object of the gem. It's used to initiate all data requests. @@ -54,10 +55,11 @@ def initialize(options = @auth_client = Fabricio::Authorization::AuthorizationClient.new session = obtain_session network_client = Fabricio::Networking::NetworkClient.new(@auth_client, @session_storage) + organization_id_provider = Fabricio::Networking::OrganizationIdProvider.new(lambda { return @app_service.all }) - @organization_service ||= Fabricio::Service::OrganizationService.new(session, network_client) - @app_service ||= Fabricio::Service::AppService.new(session, network_client) - @build_service ||= Fabricio::Service::BuildService.new(session, network_client) + @organization_service ||= Fabricio::Service::OrganizationService.new(network_client) + @app_service ||= Fabricio::Service::AppService.new(organization_id_provider, network_client) + @build_service ||= Fabricio::Service::BuildService.new(organization_id_provider, network_client) end # We use `method_missing` approach instead of explicit methods. diff --git a/lib/fabricio/models/app.rb b/lib/fabricio/models/app.rb index 7c9ea86..2203900 100644 --- a/lib/fabricio/models/app.rb +++ b/lib/fabricio/models/app.rb @@ -4,7 +4,7 @@ module Fabricio module Model # This model represents an application class App < AbstractModel - attr_reader :id, :name, :bundle_id, :created_at, :platform, :icon_url + attr_reader :id, :name, :bundle_id, :created_at, :platform, :icon_url, :organization_id # Returns an App model object # @@ -17,6 +17,7 @@ def initialize(attributes) @created_at = attributes['created_at'] @platform = attributes['platform'] @icon_url = attributes['icon_url'] + @organization_id = attributes['organization_id'] @json = attributes end diff --git a/lib/fabricio/networking/app_request_model_factory.rb b/lib/fabricio/networking/app_request_model_factory.rb index 6835810..2d7e651 100644 --- a/lib/fabricio/networking/app_request_model_factory.rb +++ b/lib/fabricio/networking/app_request_model_factory.rb @@ -15,6 +15,14 @@ class AppRequestModelFactory FABRIC_ORGANIZATIONS_ENDPOINT = '/organizations' FABRIC_PROJECTS_ENDPOINT = '/projects' + # Initializes a new AppRequestModelFactory object + # + # @param organization_id_provider [Fabricio::Networking::OrganizationIdProvider] + # @return [Fabricio::Networking::AppRequestModelFactory] + def initialize(organization_id_provider) + @organization_id_provider = organization_id_provider + end + # Returns a request model for obtaining the list of all apps # # @return [Fabricio::Networking::RequestModel] @@ -43,11 +51,10 @@ def get_app_request_model(app_id) # Returns a request model for obtaining the count of active users at the current moment # - # @param session [Fabricio::Authorization::Session] # @param app_id [String] # @return [Fabricio::Networking::RequestModel] - def active_now_request_model(session, app_id) - path = growth_analytics_endpoint(session, app_id, 'active_now') + def active_now_request_model(app_id) + path = growth_analytics_endpoint(app_id, 'active_now') model = Fabricio::Networking::RequestModel.new do |config| config.type = :GET config.base_url = FABRIC_API_URL @@ -58,13 +65,12 @@ def active_now_request_model(session, app_id) # Returns a request model for obtaining the count of daily new users # - # @param session [Fabricio::Authorization::Session] # @param app_id [String] # @param start_time [String] Timestamp of the start date # @param end_time [String] Timestamp of the end date # @return [Fabricio::Networking::RequestModel] - def daily_new_request_model(session, app_id, start_time, end_time) - path = growth_analytics_endpoint(session, app_id, 'daily_new') + def daily_new_request_model(app_id, start_time, end_time) + path = growth_analytics_endpoint(app_id, 'daily_new') params = time_range_params(start_time, end_time) model = Fabricio::Networking::RequestModel.new do |config| config.type = :GET @@ -77,14 +83,13 @@ def daily_new_request_model(session, app_id, start_time, end_time) # Returns a request model for obtaining the count of daily active users # - # @param session [Fabricio::Authorization::Session] # @param app_id [String] # @param start_time [String] Timestamp of the start date # @param end_time [String] Timestamp of the end date # @param build [String] The version of the build. E.g. '4.0.1 (38)' # @return [Fabricio::Networking::RequestModel] - def daily_active_request_model(session, app_id, start_time, end_time, build) - path = growth_analytics_endpoint(session, app_id, 'daily_active') + def daily_active_request_model(app_id, start_time, end_time, build) + path = growth_analytics_endpoint(app_id, 'daily_active') params = time_range_params(start_time, end_time) params['build'] = build model = Fabricio::Networking::RequestModel.new do |config| @@ -98,14 +103,13 @@ def daily_active_request_model(session, app_id, start_time, end_time, build) # Returns a request model for obtaining the count of weekly active users # - # @param session [Fabricio::Authorization::Session] # @param app_id [String] # @param start_time [String] Timestamp of the start date # @param end_time [String] Timestamp of the end date # @param build [String] The version of the build. E.g. '4.0.1 (38)' # @return [Fabricio::Networking::RequestModel] - def weekly_active_request_model(session, app_id, start_time, end_time, build) - path = growth_analytics_endpoint(session, app_id, 'weekly_active') + def weekly_active_request_model(app_id, start_time, end_time, build) + path = growth_analytics_endpoint(app_id, 'weekly_active') params = time_range_params(start_time, end_time) params['build'] = build model = Fabricio::Networking::RequestModel.new do |config| @@ -119,14 +123,13 @@ def weekly_active_request_model(session, app_id, start_time, end_time, build) # Returns a request model for obtaining the count of monhtly active users # - # @param session [Fabricio::Authorization::Session] # @param app_id [String] # @param start_time [String] Timestamp of the start date # @param end_time [String] Timestamp of the end date # @param build [String] The version of the build. E.g. '4.0.1 (38)' # @return [Fabricio::Networking::RequestModel] - def monthly_active_request_model(session, app_id, start_time, end_time, build) - path = growth_analytics_endpoint(session, app_id, 'monthly_active') + def monthly_active_request_model(app_id, start_time, end_time, build) + path = growth_analytics_endpoint(app_id, 'monthly_active') params = time_range_params(start_time, end_time) params['build'] = build model = Fabricio::Networking::RequestModel.new do |config| @@ -140,14 +143,13 @@ def monthly_active_request_model(session, app_id, start_time, end_time, build) # Returns a request model for obtaining the count of sessions # - # @param session [Fabricio::Authorization::Session] # @param app_id [String] # @param start_time [String] Timestamp of the start date # @param end_time [String] Timestamp of the end date # @param build [String] The version of the build. E.g. '4.0.1 (38)' # @return [Fabricio::Networking::RequestModel] - def total_sessions_request_model(session, app_id, start_time, end_time, build) - path = growth_analytics_endpoint(session, app_id, 'total_sessions_scalar') + def total_sessions_request_model(app_id, start_time, end_time, build) + path = growth_analytics_endpoint(app_id, 'total_sessions_scalar') params = { 'start' => start_time, 'end' => end_time, @@ -357,21 +359,20 @@ def issue_session_endpoint(app_id, issue_id, session_id) # Returns an API path to some growth analytic endpoint # - # @param session [Fabricio::Authorization::Session] # @param app_id [String] # @param name [String] # @return [String] - def growth_analytics_endpoint(session, app_id, name) - "#{FABRIC_API_PATH}#{org_app_endpoint(session, app_id)}/growth_analytics/#{name}.json" + def growth_analytics_endpoint(app_id, name) + "#{FABRIC_API_PATH}#{org_app_endpoint(app_id)}/growth_analytics/#{name}.json" end # Returns an API path to organization endpoint # - # @param session [Fabricio::Authorization::Session] # @param app_id [String] # @return [String] - def org_app_endpoint(session, app_id) - "#{org_endpoint(session)}/#{app_endpoint(app_id)}" + def org_app_endpoint(app_id) + organization_id = @organization_id_provider.get(app_id) + "#{org_endpoint(organization_id)}/#{app_endpoint(app_id)}" end # Returns an API path to app endpoint @@ -384,10 +385,10 @@ def app_endpoint(app_id) # Returns an API path to app endpoint # - # @param session [Fabricio::Authorization::Session] + # @param organization_id [String] # @return [String] - def org_endpoint(session) - "#{FABRIC_ORGANIZATIONS_ENDPOINT}/#{session.organization_id}" + def org_endpoint(organization_id) + "#{FABRIC_ORGANIZATIONS_ENDPOINT}/#{organization_id}" end # Returns an API path to app endpoint diff --git a/lib/fabricio/networking/build_request_model_factory.rb b/lib/fabricio/networking/build_request_model_factory.rb index ea11479..03216a9 100644 --- a/lib/fabricio/networking/build_request_model_factory.rb +++ b/lib/fabricio/networking/build_request_model_factory.rb @@ -11,13 +11,20 @@ class BuildRequestModelFactory FABRIC_APPS_ENDPOINT = '/apps' FABRIC_ORGANIZATIONS_ENDPOINT = '/organizations' + # Initializes a new BuildRequestModelFactory object + # + # @param organization_id_provider [Fabricio::Networking::OrganizationIdProvider] + # @return [Fabricio::Networking::BuildRequestModelFactory] + def initialize(organization_id_provider) + @organization_id_provider = organization_id_provider + end + # Returns a request model for obtaining the list of all builds for a specific app # - # @param session [Fabricio::Authorization::Session] # @param app_id [String] # @return [Fabricio::Networking::RequestModel] - def all_builds_request_model(session, app_id) - path = "#{FABRIC_API_PATH}#{org_app_endpoint(session, app_id)}/beta_distribution/releases" + def all_builds_request_model(app_id) + path = "#{FABRIC_API_PATH}#{org_app_endpoint(app_id)}/beta_distribution/releases" model = Fabricio::Networking::RequestModel.new do |config| config.type = :GET config.base_url = FABRIC_API_URL @@ -28,13 +35,12 @@ def all_builds_request_model(session, app_id) # Returns a request model for obtaining a specific build for a specific app # - # @param session [Fabricio::Authorization::Session] # @param app_id [String] # @param version [String] The version number. E.g. '4.0.0' # @param build_number [String] The build number. E.g. '48' # @return [Fabricio::Networking::RequestModel] - def get_build_request_model(session, app_id, version, build_number) - path = "#{FABRIC_API_PATH}#{org_app_endpoint(session, app_id)}/beta_distribution/releases" + def get_build_request_model(app_id, version, build_number) + path = "#{FABRIC_API_PATH}#{org_app_endpoint(app_id)}/beta_distribution/releases" params = { 'app[display_version]' => version, 'app[build_version]' => build_number @@ -50,13 +56,12 @@ def get_build_request_model(session, app_id, version, build_number) # Returns a request model for obtaining an array of top versions for a given app # - # @param session [Fabricio::Authorization::Session] # @param app_id [String] # @param start_time [String] Timestamp of the start date # @param end_time [String] Timestamp of the end date # @return [Fabricio::Networking::RequestModel] - def top_versions_request_model(session, app_id, start_time, end_time) - path = "#{FABRIC_API_PATH}#{org_app_endpoint(session, app_id)}/growth_analytics/top_builds" + def top_versions_request_model(app_id, start_time, end_time) + path = "#{FABRIC_API_PATH}#{org_app_endpoint(app_id)}/growth_analytics/top_builds" params = { 'app_id' => app_id, 'start' => start_time, @@ -83,19 +88,19 @@ def app_endpoint(app_id) # Returns an API path to app endpoint # - # @param session [Fabricio::Authorization::Session] + # @param organization_id [String] # @return [String] - def org_endpoint(session) - "#{FABRIC_ORGANIZATIONS_ENDPOINT}/#{session.organization_id}" + def org_endpoint(organization_id) + "#{FABRIC_ORGANIZATIONS_ENDPOINT}/#{organization_id}" end # Returns an API path to organization endpoint # - # @param session [Fabricio::Authorization::Session] # @param app_id [String] # @return [String] - def org_app_endpoint(session, app_id) - "#{org_endpoint(session)}#{app_endpoint(app_id)}" + def org_app_endpoint(app_id) + organization_id = @organization_id_provider.get(app_id) + "#{org_endpoint(organization_id)}#{app_endpoint(app_id)}" end end diff --git a/lib/fabricio/networking/organization_id_provider.rb b/lib/fabricio/networking/organization_id_provider.rb new file mode 100644 index 0000000..8a95d00 --- /dev/null +++ b/lib/fabricio/networking/organization_id_provider.rb @@ -0,0 +1,41 @@ +require 'fabricio/models/app' + +module Fabricio + module Networking + # Maps app ids to organization ids + class OrganizationIdProvider + + # Initializes a new OrganizationIdProvider object + # + # @param app_list_provider A lambda that takes no arguments and returns [Array] + # @return [Fabricio::Networking::OrganizationIdProvider] + def initialize(app_list_provider) + @app_list_provider = app_list_provider + @app_list = nil + end + + # Returns the organization id of the given app id + # + # @param app_id [String] Application identifier + # @return [String] The organization identifier to which it belongs + def get(app_id) + app_list = obtain_app_list + candidate = app_list.select do |app| + app.id == app_id + end + raise "Could not find application with id #{app_id}" unless candidate.size > 0 + candidate.first.organization_id + end + + private def obtain_app_list() + app_list = @app_list + if !app_list + app_list = @app_list_provider.call + @app_list = app_list + end + + app_list + end + end + end +end diff --git a/lib/fabricio/services/app_service.rb b/lib/fabricio/services/app_service.rb index c206af9..7150318 100644 --- a/lib/fabricio/services/app_service.rb +++ b/lib/fabricio/services/app_service.rb @@ -12,13 +12,11 @@ class AppService # Initializes a new AppService object. # - # @param session [Fabricio::Authorization::Session] + # @param organization_id_provider [Fabricio::Networking::OrganizationIdProvider] # @param network_client [Fabricio::Networking::NetworkClient] # @return [Fabricio::Service::AppService] - def initialize(session, network_client) - @session = session - - @request_model_factory = Fabricio::Networking::AppRequestModelFactory.new + def initialize(organization_id_provider, network_client) + @request_model_factory = Fabricio::Networking::AppRequestModelFactory.new(organization_id_provider) @network_client = network_client end @@ -48,7 +46,7 @@ def get(id) # @param id [String] Application identifier # @return [Integer] def active_now(id) - request_model = @request_model_factory.active_now_request_model(@session, id) + request_model = @request_model_factory.active_now_request_model(id) response = @network_client.perform_request(request_model) JSON.parse(response.body)['cardinality'] end @@ -60,7 +58,7 @@ def active_now(id) # @param end_time [String] Timestamp of the end date # @return [Array] def daily_new(id, start_time, end_time) - request_model = @request_model_factory.daily_new_request_model(@session, id, start_time, end_time) + request_model = @request_model_factory.daily_new_request_model(id, start_time, end_time) response = @network_client.perform_request(request_model) JSON.parse(response.body)['series'].map do |array| Fabricio::Model::Point.new(array) @@ -75,7 +73,7 @@ def daily_new(id, start_time, end_time) # @param build [String] The version of the build. E.g. '4.0.1 (38)' # @return [Array] def daily_active(id, start_time, end_time, build) - request_model = @request_model_factory.daily_active_request_model(@session, id, start_time, end_time, build) + request_model = @request_model_factory.daily_active_request_model(id, start_time, end_time, build) response = @network_client.perform_request(request_model) JSON.parse(response.body)['series'].map do |array| Fabricio::Model::Point.new(array) @@ -90,7 +88,7 @@ def daily_active(id, start_time, end_time, build) # @param build [String] The version of the build. E.g. '4.0.1 (38)' # @return [Array] def weekly_active(id, start_time, end_time, build) - request_model = @request_model_factory.weekly_active_request_model(@session, id, start_time, end_time, build) + request_model = @request_model_factory.weekly_active_request_model(id, start_time, end_time, build) response = @network_client.perform_request(request_model) JSON.parse(response.body)['series'].map do |array| Fabricio::Model::Point.new(array) @@ -105,7 +103,7 @@ def weekly_active(id, start_time, end_time, build) # @param build [String] The version of the build. E.g. '4.0.1 (38)' # @return [Array] def monthly_active(id, start_time, end_time, build) - request_model = @request_model_factory.monthly_active_request_model(@session, id, start_time, end_time, build) + request_model = @request_model_factory.monthly_active_request_model(id, start_time, end_time, build) response = @network_client.perform_request(request_model) JSON.parse(response.body)['series'].map do |array| Fabricio::Model::Point.new(array) @@ -120,7 +118,7 @@ def monthly_active(id, start_time, end_time, build) # @param build [String] The version of the build. E.g. '4.0.1 (38)' # @return [Integer] def total_sessions(id, start_time, end_time, build) - request_model = @request_model_factory.total_sessions_request_model(@session, id, start_time, end_time, build) + request_model = @request_model_factory.total_sessions_request_model(id, start_time, end_time, build) response = @network_client.perform_request(request_model) JSON.parse(response.body)['sessions'] end diff --git a/lib/fabricio/services/build_service.rb b/lib/fabricio/services/build_service.rb index f706370..e180149 100644 --- a/lib/fabricio/services/build_service.rb +++ b/lib/fabricio/services/build_service.rb @@ -9,13 +9,11 @@ class BuildService # Initializes a new BuildService object. # - # @param session [Fabricio::Authorization::Session] + # @param organization_id_provider [Fabricio::Networking::OrganizationIdProvider] # @param network_client [Fabricio::Networking::NetworkClient] # @return [Fabricio::Service::BuildService] - def initialize(session, network_client) - @session = session - - @request_model_factory = Fabricio::Networking::BuildRequestModelFactory.new + def initialize(organization_id_provider, network_client) + @request_model_factory = Fabricio::Networking::BuildRequestModelFactory.new(organization_id_provider) @network_client = network_client end @@ -24,7 +22,7 @@ def initialize(session, network_client) # @param app_id [String] Application identifier # @return [Array] def all(app_id) - request_model = @request_model_factory.all_builds_request_model(@session, app_id) + request_model = @request_model_factory.all_builds_request_model(app_id) response = @network_client.perform_request(request_model) JSON.parse(response.body)['instances'].map do |hash| Fabricio::Model::Build.new(hash) @@ -38,7 +36,7 @@ def all(app_id) # @param build_number [String] Build number. E.g. '39'. # @return [Fabricio::Model::Build] def get(app_id, version, build_number) - request_model = @request_model_factory.get_build_request_model(@session, app_id, version, build_number) + request_model = @request_model_factory.get_build_request_model(app_id, version, build_number) response = @network_client.perform_request(request_model) Fabricio::Model::Build.new(JSON.parse(response.body)['instances'].first) end @@ -50,7 +48,7 @@ def get(app_id, version, build_number) # @param end_time [String] Timestamp of the end date # @return [Array] def top_versions(app_id, start_time, end_time) - request_model = @request_model_factory.top_versions_request_model(@session, app_id, start_time, end_time) + request_model = @request_model_factory.top_versions_request_model(app_id, start_time, end_time) response = @network_client.perform_request(request_model) JSON.parse(response.body)['builds'] end diff --git a/lib/fabricio/services/organization_service.rb b/lib/fabricio/services/organization_service.rb index 9fb7729..0c04d7a 100644 --- a/lib/fabricio/services/organization_service.rb +++ b/lib/fabricio/services/organization_service.rb @@ -9,12 +9,9 @@ class OrganizationService # Initializes a new OrganizationService object. # - # @param session [Fabricio::Authorization::Session] # @param network_client [Fabricio::Networking::NetworkClient] # @return [Fabricio::Service::OrganizationService] - def initialize(session, network_client) - @session = session - + def initialize(network_client) @request_model_factory = Fabricio::Networking::OrganizationRequestModelFactory.new @network_client = network_client end diff --git a/spec/lib/fabricio/authorization/authorization_client_spec.rb b/spec/lib/fabricio/authorization/authorization_client_spec.rb index 698a9fa..6584e7c 100644 --- a/spec/lib/fabricio/authorization/authorization_client_spec.rb +++ b/spec/lib/fabricio/authorization/authorization_client_spec.rb @@ -8,28 +8,23 @@ TEST_STRING = 'string' TEST_TOKEN = 'token' TEST_NETWORK_TOKEN = 'network_token' - TEST_ORGANIZATION_ID = 'org_id' - TEST_NETWORK_ORGANIZATION_ID = 'network_org_id' before(:each) do @client = Fabricio::Authorization::AuthorizationClient.new @test_session = Fabricio::Authorization::Session.new({ 'access_token' => TEST_TOKEN, 'refresh_token' => TEST_TOKEN - }, TEST_ORGANIZATION_ID) + }) end it 'should return session for successful authorization' do response_file = File.new(Dir.getwd + '/spec/lib/fabricio/authorization/authorization_success_stub_response.txt') stub_request(:post, /token/).to_return(:body => response_file, :status => 200) - response_file = File.new(Dir.getwd + '/spec/lib/fabricio/authorization/organization_stub_response.txt') - stub_request(:get, /organizations/).to_return(:body => response_file, :status => 200) session = @client.auth(TEST_STRING, TEST_STRING, TEST_STRING, TEST_STRING) expect(session.access_token).to eq(TEST_NETWORK_TOKEN) expect(session.refresh_token).to eq(TEST_NETWORK_TOKEN) - expect(session.organization_id).to eq(TEST_NETWORK_ORGANIZATION_ID) end it 'should throw error on incorrect authorization response' do @@ -49,7 +44,6 @@ expect(session.access_token).to eq(TEST_NETWORK_TOKEN) expect(session.refresh_token).to eq(TEST_NETWORK_TOKEN) - expect(session.organization_id).to eq(TEST_ORGANIZATION_ID) end it 'should throw error on incorrect refresh response' do diff --git a/spec/lib/fabricio/networking/app_request_model_factory_spec.rb b/spec/lib/fabricio/networking/app_request_model_factory_spec.rb index 89f75a3..dd0fb11 100644 --- a/spec/lib/fabricio/networking/app_request_model_factory_spec.rb +++ b/spec/lib/fabricio/networking/app_request_model_factory_spec.rb @@ -6,11 +6,8 @@ describe 'AppRequestModelFactory' do before(:each) do - @factory = Fabricio::Networking::AppRequestModelFactory.new - @session = Fabricio::Authorization::Session.new({ - 'access_token' => 'token', - 'refresh_token' => 'token' - }) + @organization_id_provider = instance_double("OrganizationIdProvider", :get => '1') + @factory = Fabricio::Networking::AppRequestModelFactory.new(@organization_id_provider) end it 'should form all apps request model' do @@ -32,7 +29,7 @@ end it 'should form active_now app request model' do - result = @factory.active_now_request_model(@session, '1') + result = @factory.active_now_request_model('1') expect(result.type).to eq :GET expect(result.base_url).not_to be_nil @@ -41,7 +38,7 @@ end it 'should form daily_new app request model' do - result = @factory.daily_new_request_model(@session, '1', '1', '1') + result = @factory.daily_new_request_model('1', '1', '1') expect(result.type).to eq :GET expect(result.base_url).not_to be_nil @@ -50,7 +47,7 @@ end it 'should form daily_active app request model' do - result = @factory.daily_active_request_model(@session, '1', '1', '1', '1') + result = @factory.daily_active_request_model('1', '1', '1', '1') expect(result.type).to eq :GET expect(result.base_url).not_to be_nil @@ -59,7 +56,7 @@ end it 'should form monthly_active app request model' do - result = @factory.monthly_active_request_model(@session, '1', '1', '1', '1') + result = @factory.monthly_active_request_model('1', '1', '1', '1') expect(result.type).to eq :GET expect(result.base_url).not_to be_nil @@ -68,7 +65,7 @@ end it 'should form session_count app request model' do - result = @factory.total_sessions_request_model(@session, '1', '1', '1', '1') + result = @factory.total_sessions_request_model('1', '1', '1', '1') expect(result.type).to eq :GET expect(result.base_url).not_to be_nil diff --git a/spec/lib/fabricio/networking/build_request_model_factory_spec.rb b/spec/lib/fabricio/networking/build_request_model_factory_spec.rb index 27c9fbd..293a4d4 100644 --- a/spec/lib/fabricio/networking/build_request_model_factory_spec.rb +++ b/spec/lib/fabricio/networking/build_request_model_factory_spec.rb @@ -6,15 +6,12 @@ describe 'BuildRequestModelFactory' do before(:each) do - @factory = Fabricio::Networking::BuildRequestModelFactory.new - @session = Fabricio::Authorization::Session.new({ - 'access_token' => 'token', - 'refresh_token' => 'token' - }) + @organization_id_provider = instance_double("OrganizationIdProvider", :get => '1') + @factory = Fabricio::Networking::BuildRequestModelFactory.new(@organization_id_provider) end it 'should form all builds request model' do - result = @factory.all_builds_request_model(@session, '1') + result = @factory.all_builds_request_model('1') expect(result.type).to eq :GET expect(result.base_url).not_to be_nil @@ -23,7 +20,7 @@ end it 'should form get build request model' do - result = @factory.get_build_request_model(@session, '1', '1', '1') + result = @factory.get_build_request_model('1', '1', '1') expect(result.type).to eq :GET expect(result.base_url).not_to be_nil @@ -33,7 +30,7 @@ end it 'should form top versions request model' do - result = @factory.top_versions_request_model(@session, '1', '1', '1') + result = @factory.top_versions_request_model('1', '1', '1') expect(result.type).to eq :GET expect(result.base_url).not_to be_nil @@ -41,4 +38,4 @@ expect(result.headers).not_to be_nil expect(result.params).not_to be_nil end -end \ No newline at end of file +end diff --git a/spec/lib/fabricio/networking/network_client_spec.rb b/spec/lib/fabricio/networking/network_client_spec.rb index fabf80d..9807b81 100644 --- a/spec/lib/fabricio/networking/network_client_spec.rb +++ b/spec/lib/fabricio/networking/network_client_spec.rb @@ -19,7 +19,7 @@ session = Fabricio::Authorization::Session.new({ 'access_token' => TEST_TOKEN, 'refresh_token' => '123' - }, '123') + }) @storage.store_session(session) end diff --git a/spec/lib/fabricio/networking/organization_id_provider_spec.rb b/spec/lib/fabricio/networking/organization_id_provider_spec.rb new file mode 100644 index 0000000..dbaccad --- /dev/null +++ b/spec/lib/fabricio/networking/organization_id_provider_spec.rb @@ -0,0 +1,34 @@ +require 'rspec' +require 'fabricio/networking/organization_id_provider' +require 'fabricio/models/app' + +describe 'OrganizationIdProvider' do + + SAMPLE_ORGANIZATION_ID = '2' + + before(:each) do + @sample_app = Fabricio::Model::App.new({ + 'organization_id' => SAMPLE_ORGANIZATION_ID, + 'id' => '1' + }) + @app_list_provider = spy("App list provider", :call => [@sample_app]) + @organization_id_provider = Fabricio::Networking::OrganizationIdProvider.new(@app_list_provider) + end + + it 'should not obtain app list automatically' do + expect(@app_list_provider).to_not have_received(:call) + end + + it 'should obtain app list once on demand' do + @organization_id_provider.get("1") + @organization_id_provider.get("1") + + expect(@app_list_provider).to have_received(:call).once + end + + it 'should return the correct organization id' do + organization_id = @organization_id_provider.get("1") + + expect(organization_id).to eq SAMPLE_ORGANIZATION_ID + end +end diff --git a/spec/lib/fabricio/service/app_service_spec.rb b/spec/lib/fabricio/service/app_service_spec.rb index 1259534..a91841b 100644 --- a/spec/lib/fabricio/service/app_service_spec.rb +++ b/spec/lib/fabricio/service/app_service_spec.rb @@ -11,10 +11,11 @@ session = Fabricio::Authorization::Session.new({ 'access_token' => '123', 'refresh_token' => '123' - }, '123') + }) storage.store_session(session) client = Fabricio::Networking::NetworkClient.new(nil, storage) - @service = Fabricio::Service::AppService.new(Fabricio::Authorization::Session.new, client) + organization_id_provider = instance_double("OrganizationIdProvider", :get => '1') + @service = Fabricio::Service::AppService.new(organization_id_provider, client) end it 'should fetch all apps' do diff --git a/spec/lib/fabricio/service/build_service_spec.rb b/spec/lib/fabricio/service/build_service_spec.rb index 487d773..2220aec 100644 --- a/spec/lib/fabricio/service/build_service_spec.rb +++ b/spec/lib/fabricio/service/build_service_spec.rb @@ -10,10 +10,11 @@ session = Fabricio::Authorization::Session.new({ 'access_token' => '123', 'refresh_token' => '123' - }, '123') + }) storage.store_session(session) client = Fabricio::Networking::NetworkClient.new(nil, storage) - @service = Fabricio::Service::BuildService.new(Fabricio::Authorization::Session.new, client) + organization_id_provider = instance_double("OrganizationIdProvider", :get => '1') + @service = Fabricio::Service::BuildService.new(organization_id_provider, client) end it 'should fetch all builds' do diff --git a/spec/lib/fabricio/service/organization_service_spec.rb b/spec/lib/fabricio/service/organization_service_spec.rb index 8c5bb11..3e87b82 100644 --- a/spec/lib/fabricio/service/organization_service_spec.rb +++ b/spec/lib/fabricio/service/organization_service_spec.rb @@ -10,10 +10,10 @@ session = Fabricio::Authorization::Session.new({ 'access_token' => '123', 'refresh_token' => '123' - }, '123') + }) storage.store_session(session) client = Fabricio::Networking::NetworkClient.new(nil, storage) - @service = Fabricio::Service::OrganizationService.new(Fabricio::Authorization::Session.new, client) + @service = Fabricio::Service::OrganizationService.new(client) end it 'should fetch organization' do