Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CognitiveDisson committed Feb 19, 2018
1 parent 53d1f74 commit f740a91
Show file tree
Hide file tree
Showing 31 changed files with 475 additions and 239 deletions.
1 change: 1 addition & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ ratings:
- "**.rb"
exclude_paths:
- spec/
- lib/cli/
42 changes: 42 additions & 0 deletions lib/fabricio/authorization/abstract_param_storage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Fabricio
module Authorization
# A class providing an interface for implementing Fabric session storage. Subclass it to provide your own behaviour (e.g. storing session data in database)
class AbstractParamStorage

# Returns all stored variable
#
# @return [Hash]
def obtain
raise NotImplementedError, "Implement this method in a child class"
end

# Save variable
#
# @param hash [Hash]
def store(hash)
raise NotImplementedError, "Implement this method in a child class"
end

# Resets current state and deletes all saved params
def reset_all
raise NotImplementedError, "Implement this method in a child class"
end

def organization_id
raise NotImplementedError, "Implement this method in a child class"
end

def app_id
raise NotImplementedError, "Implement this method in a child class"
end

def store_organization_id(organization_id)
raise NotImplementedError, "Implement this method in a child class"
end

def store_app_id(app_id)
raise NotImplementedError, "Implement this method in a child class"
end
end
end
end
23 changes: 2 additions & 21 deletions lib/fabricio/authorization/authorization_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,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
Expand All @@ -71,7 +70,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.
Expand Down Expand Up @@ -99,24 +98,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
64 changes: 64 additions & 0 deletions lib/fabricio/authorization/file_param_storage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'fabricio/authorization/abstract_param_storage'
require 'fileutils'
require 'yaml'

# Constants
FABRICIO_DIRECTORY_PATH = "#{Dir.home}/.fabricio"
PARAM_FILE_PATH = "#{CREDENTIAL_DIRECTORY_PATH}/.params"

module Fabricio
module Authorization
# Stores default params as organization, app, etc.
class FileParamStorage < AbstractParamStorage

# Returns all stored variable
#
# @return [Hash]
def obtain
return nil unless File.exist?(PARAM_FILE_PATH)
return YAML.load_file(PARAM_FILE_PATH) || {}
end

# Save variable
#
# @param hash [Hash]
def store(hash)
save_to_file(hash)
end

# Resets current state and deletes all saved params
def reset_all
FileUtils.remove_file(PARAM_FILE_PATH)
end

def organization_id
obtain['organization_id']
end

def app_id
obtain['app_id']
end

def store_organization_id(organization_id)
hash = obtain || {}
hash['organization_id'] = organization_id
save_to_file(hash)
end

def store_app_id(app_id)
hash = obtain || {}
hash['app_id'] = app_id
save_to_file(hash)
end

private

def save_to_file(hash)
FileUtils.mkdir_p(FABRICIO_DIRECTORY_PATH)
File.open(SESSION_FILE_PATH,'w') do |f|
f.write hash.to_yaml
end
end
end
end
end
42 changes: 42 additions & 0 deletions lib/fabricio/authorization/file_session_storage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'fabricio/authorization/abstract_session_storage'
require 'fileutils'
require 'yaml'

# Constants
FABRICIO_DIRECTORY_PATH = "#{Dir.home}/.fabricio"
SESSION_FILE_PATH = "#{CREDENTIAL_DIRECTORY_PATH}/.session"

module Fabricio
module Authorization
# The only one built-in session storage. Stores current session in file.
class FileSessionStorage < AbstractSessionStorage

# Returns session stored in a variable
#
# @return [Fabricio::Authorization::Session]
def obtain_session
return nil unless File.exist?(SESSION_FILE_PATH)
session_hash = YAML.load_file(SESSION_FILE_PATH)
session = Session(session_hash)
return nil unless session.access_token
return nil unless session.refresh_token
session
end

# Stores session in a variable
#
# @param session [Fabricio::Authorization::MemorySessionStorage]
def store_session(session)
FileUtils.mkdir_p(FABRICIO_DIRECTORY_PATH)
File.open(SESSION_FILE_PATH,'w') do |f|
f.write session.hash.to_yaml
end
end

# Resets current state and deletes saved session
def reset
FileUtils.remove_file(SESSION_FILE_PATH)
end
end
end
end
55 changes: 55 additions & 0 deletions lib/fabricio/authorization/memory_param_storage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'fabricio/authorization/abstract_param_storage'

module Fabricio
module Authorization
# Stores default params as organization, app, etc.
class MemoryParamStorage < AbstractParamStorage

# Initializes a new ParamStorage object
#
# @return [Fabricio::Authorization::ParamStorage]
def initialize
@params = nil
end

# Returns all stored variable
#
# @return [Hash]
def obtain
@params
end

# Save variable
#
# @param hash [Hash]
def store(hash)
@params = hash
end

# Resets current state and deletes all saved params
def reset_all
@params = nil
end

def organization_id
obtain['organization_id']
end

def app_id
obtain['app_id']
end

def store_organization_id(organization_id)
hash = obtain || {}
hash['organization_id'] = organization_id
@params = hash
end

def store_app_id(app_id)
hash = obtain || {}
hash['app_id'] = app_id
@params = hash
end
end
end
end
2 changes: 1 addition & 1 deletion lib/fabricio/authorization/memory_session_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def reset
end
end
end
end
end
14 changes: 10 additions & 4 deletions lib/fabricio/authorization/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ 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

def hash
return {} unless session.access_token && session.refresh_token
return {
'access_token' => session.access_token,
'refresh_token' => session.refresh_token
}
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/fabricio/cli/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
require 'fabricio'
require 'fileutils'
require 'yaml'
require 'fabricio/authorization/file_session_storage'
require_relative 'organization'
require_relative 'app'
require_relative 'build'
require_relative 'version'
require_relative 'cli_helper'


module Fabricio
class CLI < Thor

Expand All @@ -31,12 +33,14 @@ def credential
tmp_client = Fabricio::Client.new do |config|
config.username = credential.email
config.password = credential.password
config.session_storage = FileSessionStorage()
end

say("Your session store in #{SESSION_FILE_PATH}")

organization = tmp_client.organization.get
unless organization.nil?
say("Successful login to #{organization.name}")
create_credential_file(credential)
say("Successful login")
else
say("Login failed")
end
Expand Down
34 changes: 7 additions & 27 deletions lib/fabricio/cli/cli_helper.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,19 @@
require 'fabricio'
require 'fabricio/authorization/file_session_storage'
require 'fileutils'
require 'yaml'

# Constants
CREDENTIAL_DIRECTORY_PATH = "#{Dir.home}/.fabricio"
CREDENTIAL_FILE_PATH = "#{CREDENTIAL_DIRECTORY_PATH}/.credential"
FABRIC_GRAPHQL_API_URL = 'https://api-dash.fabric.io/graphql'

def client
email = ""
password = ""
if File.file?(CREDENTIAL_FILE_PATH)
credential = YAML.load_file(CREDENTIAL_FILE_PATH)
email = credential['email']
password = credential['password']
else
ask_credential
end
sessionStorage = FileSessionStorage()
session = sessionStorage.obtain_session
ask_credential unless session

client = Fabricio::Client.new do |config|
config.username = email
config.password = password
end
end

def create_credential_file(credential)
FileUtils.mkdir_p(CREDENTIAL_DIRECTORY_PATH)
credential_hash = {
"email" => credential.email,
"password" => credential.password
}
File.open(CREDENTIAL_FILE_PATH,'w') do |f|
f.write credential_hash.to_yaml
config.session_storage = sessionStorage
config.param_storage = FileParamStorage()
end
say("Your credential in #{CREDENTIAL_FILE_PATH}")
end

def ask_credential
Expand All @@ -43,4 +23,4 @@ def ask_credential
password = ask("password: ", :echo => false)
say("")
Fabricio::Model::Credential.new(email, password)
end
end
Loading

0 comments on commit f740a91

Please sign in to comment.