Updated tests after renaming REPP API accounts controller

This commit is contained in:
Sergei Tsõganov 2022-06-14 11:27:37 +03:00
parent a44ac5359d
commit 1b14a9d012
5 changed files with 16 additions and 16 deletions

View file

@ -0,0 +1,70 @@
require 'test_helper'
class ReppV1AccountsActivitiesListTest < ActionDispatch::IntegrationTest
def setup
@user = users(:api_bestnames)
token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}")
token = "Basic #{token}"
@auth_headers = { 'Authorization' => token }
end
def test_returns_account_activities
get repp_v1_accounts_path, headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal @user.registrar.cash_account.activities.count, json[:data][:count]
assert_equal @user.registrar.cash_account.activities.count, json[:data][:activities].length
assert json[:data][:activities][0].is_a? Hash
end
def test_respects_limit
get repp_v1_accounts_path(limit: 1), headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal 1, json[:data][:activities].length
end
def test_respects_offset
offset = 1
get repp_v1_accounts_path(offset: offset), headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal (@user.registrar.cash_account.activities.count - offset), json[:data][:activities].length
end
def test_returns_account_activities_by_search_query
search_params = {
description_matches: '%renew%',
}
get repp_v1_accounts_path(q: search_params), headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal json[:data][:activities].length, 1
assert json[:data][:activities][0].is_a? Hash
end
def test_returns_account_activities_by_sort_query
activity = account_activities(:renew_cash)
sort_params = {
s: 'activity_type asc',
}
get repp_v1_accounts_path(q: sort_params), headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal @user.registrar.cash_account.activities.count, json[:data][:count]
assert_equal @user.registrar.cash_account.activities.count, json[:data][:activities].length
assert_equal json[:data][:activities][0][:description], activity.description
end
end

View file

@ -0,0 +1,53 @@
require 'test_helper'
class ReppV1BalanceTest < ActionDispatch::IntegrationTest
def setup
travel_to Time.zone.parse('2010-07-05')
@registrar = users(:api_bestnames)
token = Base64.encode64("#{@registrar.username}:#{@registrar.plain_text_password}")
token = "Basic #{token}"
@auth_headers = { 'Authorization' => token }
end
def test_can_query_balance
get '/repp/v1/accounts/balance', headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal 1000, json[:code]
assert_equal 'Command completed successfully', json[:message]
assert_equal @registrar.registrar.cash_account.balance.to_s, json[:data][:balance]
assert_equal @registrar.registrar.cash_account.currency, json[:data][:currency]
end
def test_can_query_balance_with_details
# Create new billable action to get activity
post "/repp/v1/domains/renew/bulk", headers: @auth_headers, params: { domains: ['shop.test'], renew_period: '1y' }
started_from = "2010-07-05"
end_to = DateTime.current.to_date.to_s(:db)
get "/repp/v1/accounts/balance?detailed=true", headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal 1000, json[:code]
assert_equal 'Command completed successfully', json[:message]
assert_equal @registrar.registrar.cash_account.balance.to_s, json[:data][:balance]
assert_equal @registrar.registrar.cash_account.currency, json[:data][:currency]
entry = json[:data][:transactions].last
assert_equal @registrar.registrar.cash_account.account_activities.last.created_at, entry[:created_at]
assert_equal @registrar.registrar.cash_account.account_activities.last.description, entry[:description]
assert_equal 'debit', entry[:type]
assert_equal @registrar.registrar.cash_account.account_activities.last.sum.to_s, entry[:sum]
assert_equal @registrar.registrar.cash_account.account_activities.last.new_balance.to_s, entry[:balance]
json[:data][:transactions].map do |trans|
assert trans[:created_at].to_date.to_s(:db) >= started_from
assert trans[:created_at].to_date.to_s(:db) >= end_to
end
end
end

View file

@ -0,0 +1,22 @@
require 'test_helper'
class ReppV1AccountsDetailsTest < ActionDispatch::IntegrationTest
def setup
@user = users(:api_bestnames)
token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}")
token = "Basic #{token}"
@auth_headers = { 'Authorization' => token }
end
def test_returns_account_details
get '/repp/v1/accounts/details', headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal 1000, json[:code]
assert_equal 'Command completed successfully', json[:message]
assert_equal @user.registrar.billing_email, json[:data][:account][:billing_email]
end
end

View file

@ -0,0 +1,69 @@
require 'test_helper'
class ReppV1AccountsUpdateAutoReloadBalanceTest < ActionDispatch::IntegrationTest
def setup
@user = users(:api_bestnames)
token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}")
token = "Basic #{token}"
@auth_headers = { 'Authorization' => token }
end
def test_updates_auto_reload_balance
amount = 100
threshold = 10
request_body = {
type: {
amount: amount,
threshold: threshold,
},
}
assert_nil @user.registrar.settings['balance_auto_reload']
post '/repp/v1/accounts/update_auto_reload_balance', headers: @auth_headers,
params: request_body
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal 1000, json[:code]
assert_equal 'Balance Auto-Reload setting has been updated', json[:message]
@user.registrar.reload
assert_equal amount, @user.registrar.settings['balance_auto_reload']['type']['amount']
assert_equal threshold, @user.registrar.settings['balance_auto_reload']['type']['threshold']
end
def test_returns_error_if_type_has_wrong_attributes
min_deposit = 10
request_body = {
type: {
amount: 5,
threshold: -1,
},
}
Setting.minimum_deposit = min_deposit
post '/repp/v1/accounts/update_auto_reload_balance', headers: @auth_headers,
params: request_body
json = JSON.parse(response.body, symbolize_names: true)
assert_response :bad_request
amount_error = "Amount must be greater than or equal to #{min_deposit.to_f}"
threshold = 'Threshold must be greater than or equal to 0'
assert_equal "#{amount_error}, #{threshold}", json[:message]
end
def test_disables_auto_reload_balance
get '/repp/v1/accounts/disable_auto_reload_balance', headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal 1000, json[:code]
assert_equal 'Balance Auto-Reload setting has been disabled', json[:message]
@user.registrar.reload
assert_nil @user.registrar.settings['balance_auto_reload']
end
end

View file

@ -0,0 +1,30 @@
require 'test_helper'
class ReppV1AccountsUpdateDetailsTest < ActionDispatch::IntegrationTest
def setup
@user = users(:api_bestnames)
token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}")
token = "Basic #{token}"
@auth_headers = { 'Authorization' => token }
end
def test_updates_details
request_body = {
account: {
billing_email: 'donaldtrump@yandex.ru',
iban: 'GB331111111111111111',
},
}
put '/repp/v1/accounts', headers: @auth_headers, params: request_body
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal 1000, json[:code]
assert_equal 'Your account has been updated', json[:message]
assert_equal(request_body[:account][:billing_email], @user.registrar.billing_email)
assert_equal(request_body[:account][:iban], @user.registrar.iban)
end
end