Skip to content

Instantly share code, notes, and snippets.

@doomspork
Created March 1, 2013 03:59
Show Gist options
  • Save doomspork/5062375 to your computer and use it in GitHub Desktop.
Save doomspork/5062375 to your computer and use it in GitHub Desktop.
Sample non-managed currency implementation with unit test
require 'sinatra'
#The Secret Key for your App ID
secret_key = ''
get '/deposit' do
#Get the parameter `id`
id = params[:id]
#Get `currency` parameter
currency = params[:currency]
#Get `snuid` which should equal what was set by the SDK's `setUserId`
snuid = params[:snuid]
#Get Tapjoy's generated verifier
verifier = params[:verifier]
#Compute a new verifier with our Secret Key and parameters
computed_verifier = Digest::MD5.hexdigest("#{id}:#{snuid}:#{currency}:#{secret_key}")
#Does the supplied verifier and our computed one match?
if verifier.eql? computed_verifier
#Valid, save currency
#Return 200
status 200
else
#Somethings not right
#Return 403
status 403
end
end
#!/usr/local/bin/ruby
require 'rubygems'
require 'trollop'
require 'digest/md5'
require 'test/unit'
require 'faraday'
raise RuntimeError, "**A command line option must be supplied!**" if ARGV.empty?
$opts = Trollop::options do
banner "Tapjoy Non-Managed Currency Test"
opt :callback, "Callback URL", :short => 'c', :type => String
opt :secretkey, "Tapjoy Secret Key", :short => 's', :type => String
end
Trollop::die :callback, "must exist" unless $opts[:callback]
Trollop::die :secretkey, "must exist" unless $opts[:secretkey]
class TestNonManagedCurrency < Test::Unit::TestCase
def setup
@connection = Faraday.new(:url => $opts[:callback]) do |faraday|
faraday.request :url_encoded
faraday.response :logger
faraday.adapter Faraday.default_adapter
end
snuid = 'testuser'
secret_key = $opts[:secretkey]
id = fast_token
currency = 1 + Random.rand(20)
verifier = Digest::MD5.hexdigest("#{id}:#{snuid}:#{currency}:#{secret_key}")
@valid_params = {:snuid => snuid, :currency => currency, :verifier => verifier, :id => id}
end
def test_success
response = @connection.get do |req|
req.params = @valid_params
end
status = response.status
assert(status.eql?(200), "Should be a valid request")
end
def test_invalid_verifier
invalid_params = @valid_params.to_hash
invalid_params[:snuid] = 'invaliduser'
response = @connection.get do |req|
req.params = invalid_params
end
status = response.status
assert(status.eql?(403), "Should be an invalid request")
end
def test_missing_param
missing_param = @valid_params.to_hash
missing_param.delete(:id)
response = @connection.get do |req|
req.params = missing_param
end
status = response.status
assert(status.eql?(403), "Should be an invalid request")
end
def fast_token
values = [
rand(0x0010000),
rand(0x0010000),
rand(0x0010000),
rand(0x0010000),
rand(0x0010000),
rand(0x1000000),
rand(0x1000000),
]
"%04x%04x%04x%04x%04x%06x%06x" % values
end
end
source :rubygems
gem 'sinatra'
gem 'trollop'
gem 'faraday'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment