Skip to content

Commit

Permalink
Merge branch 'release/0.0.08'
Browse files Browse the repository at this point in the history
  • Loading branch information
kgathi2 committed May 26, 2015
2 parents 761b3ec + 27fc3e6 commit bdc0cee
Show file tree
Hide file tree
Showing 32 changed files with 166 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/pkg/
/spec/reports/
/tmp/
/lib/mpayer_ruby/support/fake_mpayer/**/*.json
1 change: 1 addition & 0 deletions Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
guard :minitest do
# with Minitest::Unit
watch(%r{^test/(.*)\/?(.*)_test\.rb$})
watch(%r{^test/support/(.*)\/?(.*)\.*$})
watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
watch(%r{^test/test_helper\.rb$}) { 'test' }

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,22 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Testing
mpayer_ruby has used `webmock` gem to stub out real request to mpayer.co.ke. In order to make tests work, first generate the json api on file using the rake task

$ rake load:mpayer

This will generate local copies of mpayer endpoints for testing. Should work well for unit testing.

WHen you want to run test against real mpayer.co.ke for real responses, run it with the environmental variable `CI_TEST=ON` as follows

$ CI_TEST=ON rake test

or

$ CI_TEST=ON bundle exec guard


## Todo
1. Add Webmock for faster / localised test or sinatra app method
2. Add versioning
Expand Down
11 changes: 11 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,15 @@ Rake::TestTask.new do |task|
task.pattern = 'test/**/*_test.rb'
end

namespace :load do
desc "Load Mpayer to local from Test run for offline testing"
task :mpayer do
# Delete current files first
File.delete(*Dir.glob('lib/mpayer_ruby/support/**/*.json'))
ENV['LOAD_MPAYER'] = 'true'
Rake::Task["test"].invoke
end
end


task :default => :test
17 changes: 0 additions & 17 deletions lib/mpayer/fetch.rb

This file was deleted.

3 changes: 0 additions & 3 deletions lib/mpayer/version.rb

This file was deleted.

16 changes: 8 additions & 8 deletions lib/mpayer_ruby.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require "wsse"
require "httparty"
require "hashie"
require "mpayer/version"
require "mpayer/configuration"
require "mpayer/fetch"
require "mpayer/endpoints/endpoint"
require "mpayer/endpoints/client"
require "mpayer/endpoints/transaction"
require "mpayer/endpoints/account"
require "mpayer/endpoints/payable"
require "mpayer_ruby/version"
require "mpayer_ruby/configuration"
require "mpayer_ruby/fetch"
require "mpayer_ruby/endpoints/endpoint"
require "mpayer_ruby/endpoints/client"
require "mpayer_ruby/endpoints/transaction"
require "mpayer_ruby/endpoints/account"
require "mpayer_ruby/endpoints/payable"

# begin
# require "pry"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
42 changes: 42 additions & 0 deletions lib/mpayer_ruby/fetch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Mpayer
class Fetch
include HTTParty
# base_uri "http://beta.json-generator.com"
base_uri "https://app.mpayer.co.ke/api/"
parser proc {|data| Hashie::Mash.new(response: (JSON.parse(data) rescue {data:data}.to_json) ).response}
format :json
headers

class << self
def headers
Mpayer.configuration.header.merge!(super)
end

%w(get put post delete).each do |m|
define_method m do |path, options={}, &block|
res = perform_request Net::HTTP::Put, path, options, &block if m == 'put'
res = perform_request Net::HTTP::Get, path, options, &block if m == 'get'
res = perform_request Net::HTTP::Post, path, options, &block if m == 'post'
res = perform_request Net::HTTP::Delete, path, options, &block if m == 'delete'
save_json(res) if load_json?
res
end
end

def save_json(response)
request_method = response.request.http_method.name.split('::').last.upcase
file_path = response.request.path.to_s
slash,model,*file_name = file_path.split('/')
file_location = "lib/mpayer_ruby/support/fake_mpayer/#{model}/#{request_method}_#{file_name.join('_')}.json"
File.write(file_location, response.body)
end

def load_json?
# load JSON if WebMock is off while testing and only in local env with load mpayer turned on
defined?(WebMock).nil? and ENV['CI_TEST'].nil? and defined?(Minitest) and ENV['LOAD_MPAYER'] == "true"
end

end

end
end
25 changes: 25 additions & 0 deletions lib/mpayer_ruby/support/fake_mpayer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'sinatra/base'
require 'tilt/erb'

class FakeMpayer < Sinatra::Base

[ :get, :post, :put, :delete ].each do |method|
send method, /.*/ do
if (request.env['HTTP_X_WSSE'].empty? rescue true)
[200, {}, [{base:["Authentication Failed"]}.to_json]]
else
slash,api,model,*path = request.path_info.split('/')
json_response 200, "#{model}/#{request.request_method}_#{path.join('_')}.json"
end
end
end

private

def json_response(response_code, file_name)
content_type :json
status response_code
headers "content-type"=>["application/json; charset=utf-8"]
erb File.open(File.dirname(__FILE__) + '/fake_mpayer/' + file_name, 'rb').read
end
end
Empty file.
Empty file.
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions lib/mpayer_ruby/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Mpayer
VERSION = "0.0.08"
end
4 changes: 3 additions & 1 deletion mpayer_ruby.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'mpayer/version'
require 'mpayer_ruby/version'

Gem::Specification.new do |spec|
spec.name = "mpayer_ruby"
Expand Down Expand Up @@ -38,6 +38,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "pry-nav", "~> 0.2"
spec.add_development_dependency "pry-alias", "~> 0.0"
spec.add_development_dependency "coveralls", '~> 0'
spec.add_development_dependency "webmock", '~> 1.21'
spec.add_development_dependency "sinatra", '~> 1.4'

spec.add_dependency 'httparty', "~> 0.13"
spec.add_dependency 'wsse', "~> 0.0"
Expand Down
15 changes: 0 additions & 15 deletions test/mpayer/fetch_test.rb

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_update_account
# skip
account = Mpayer::Account.find(25735)
new_name = account.name.next
account.update(name:new_name)
updated_account = account.update(name:new_name)
assert_equal(new_name, account.name)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_client_find_and_all
end

def test_create_client
skip skip
# skip skip
client_attributes = {
client: {
client_name: "Kiki Lolo",
Expand All @@ -40,33 +40,38 @@ def test_get_client_account
# skip
client = Mpayer::Client.find(26, fetch:false)
accounts = client.accounts(per:1,per_page:100)
account = client.account(accounts.first.id)
account = client.account(25774)
assert(account.is_a?(Hash), "Failure message.")
end

def test_create_new_account
skip skip
# skip skip
client = Mpayer::Client.find(26, fetch:false)
account_options = {
account:{
name: 'PZ alias Account',
ac_type: 'cu',
mandate: 's',
aliases_attributes: [{org_id:2 ,alias_key:'telephone', alias_value:'pz_test3'}]
aliases_attributes: [{org_id:2 ,alias_key:'telephone', alias_value:"pz_test#{rand(10000)}"}]
# tags_attributes:@tags,
# infos_attributes:@infos
}
}
# binding.pry
# bp
account = client.create_account(account_options)
refute_nil(account, "Failure message.")
assert_equal(account, client.account)
end

def test_get_client_accounts
def test_get_client_accounts_and_transactions
# skip
accounts = Mpayer::Client.find(26, fetch:false).accounts
assert(accounts.is_a?(Array), "Failure message.")

account_id = accounts.last.id
transactions = Mpayer::Client.find(26, fetch:false).transactions(account_id)
assert(transactions.is_a?(Array), "Failure message.")

end

def test_get_client_payables
Expand All @@ -75,11 +80,4 @@ def test_get_client_payables
assert(accounts.is_a?(Array), "Failure message.")
end

def test_get_client_account_transactions
# skip
account_id = 87 # account = client.accounts.last.id
transactions = Mpayer::Client.find(26, fetch:false).transactions(account_id)
assert(transactions.is_a?(Array), "Failure message.")
end

end
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions test/mpayer_ruby/fetch_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'test_helper'

class TestMpayerFetch < Minitest::Test
def test_unauthorised
# skip
url = "/clients/12345"
request = Mpayer::Fetch.get(url,headers:{"X-WSSE"=>""})
assert(request.base, "Failure message.")
assert_equal(["Authentication Failed"], request.base)

url = "/clients/12345"
request = Mpayer::Fetch.get(url,headers:{})
assert(request.base, "Failure message.")
assert_equal(["Authentication Failed"], request.base)
end
end
17 changes: 17 additions & 0 deletions test/mpayer_ruby/support/fake_mpayer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'rubygems'
require 'pry'
require "pry-alias"
require 'mpayer_ruby'

Mpayer.setup do |config|
config.user_no = ENV['MPAYER_USER']
config.token = ENV['MPAYER_TOKEN']
end

def save_json(response)
request_method = response.request.http_method.name.split('::').last.upcase
file_path = response.request.path.to_s
slash,model,*file_name = file_path.split('/')
file_location = "test/support/fake_mpayer/#{model}/#{request_method}_#{file_name.join('_')}.json"
File.write(file_location, response.body)
end
File renamed without changes.
11 changes: 11 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,25 @@
require 'pry'
require "pry-alias"



if ENV['CI_TEST'].nil? and ENV['LOAD_MPAYER'] != "true"
require 'webmock/minitest'
WebMock.disable_net_connect!(allow_localhost: true) #,:allow => "app.mpayer.co.ke")
require 'mpayer_ruby/support/fake_mpayer'
end

# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # spec-like progress

class Minitest::Test

def setup
Mpayer.setup do |config|
# Setup test with Mpayer Demo account.
config.user_no = ENV['MPAYER_USER']
config.token = ENV['MPAYER_TOKEN']
end
# https://robots.thoughtbot.com/how-to-stub-external-services-in-tests
stub_request(:any, /app.mpayer.co.ke/).to_rack(FakeMpayer) if ENV['CI_TEST'].nil? and ENV['LOAD_MPAYER'] != "true"
end
end

0 comments on commit bdc0cee

Please sign in to comment.