1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/test/integration/wallets_test.rb

79 lines
2.4 KiB
Ruby
Raw Normal View History

2016-07-18 18:16:54 +02:00
class WalletsTest < ActionDispatch::IntegrationTest
# Called before every test method runs. Can be used
# to set up fixture information.
def setup
2016-07-11 11:40:20 +02:00
@vlonchamp = User.find_by(username: 'vlonchamp')
login_as(@vlonchamp, scope: :user)
2016-07-18 18:16:54 +02:00
end
# Called after every test method runs. Can be used to tear
# down fixture information.
def teardown
# Do nothing
end
test 'get my wallet' do
2016-07-11 11:40:20 +02:00
get "/api/wallet/by_user/#{@vlonchamp.id}"
2016-07-18 18:16:54 +02:00
assert_equal 200, response.status
assert_equal Mime::JSON, response.content_type
wallet = json_response(response.body)
2016-07-11 11:40:20 +02:00
assert_equal @vlonchamp.wallet.user_id, wallet[:user_id]
assert_equal @vlonchamp.wallet.amount, wallet[:amount]
2016-07-18 18:16:54 +02:00
end
test 'admin can get wallet by user id' do
@admin = User.find_by_username('admin')
login_as(@admin, scope: :user)
@user1 = User.first
get "/api/wallet/by_user/#{@user1.id}"
assert_equal 200, response.status
assert_equal Mime::JSON, response.content_type
wallet = json_response(response.body)
assert_equal @user1.wallet.user_id, wallet[:user_id]
assert_equal @user1.wallet.amount, wallet[:amount]
end
2016-07-05 13:20:25 +02:00
test 'cant get wallet of an user if not admin' do
2016-07-11 11:40:20 +02:00
user5 = users(:user_4)
2016-07-05 13:20:25 +02:00
get "/api/wallet/by_user/#{user5.id}"
assert_equal 403, response.status
end
test 'get all transactions of wallet' do
2016-07-11 11:40:20 +02:00
w = @vlonchamp.wallet
2016-07-05 13:20:25 +02:00
get "/api/wallet/#{w.id}/transactions"
assert_equal 200, response.status
assert_equal Mime::JSON, response.content_type
transactions = json_response(response.body)
assert_equal w.wallet_transactions.count, transactions.size
assert_equal wallet_transactions(:transaction1).id, transactions.first[:id]
end
test 'only admin and wallet owner can show their transactions' do
2016-07-11 11:40:20 +02:00
user5 = users(:user_4)
2016-07-05 13:20:25 +02:00
get "/api/wallet/#{user5.wallet.id}/transactions"
2016-07-18 18:16:54 +02:00
assert_equal 403, response.status
end
test 'admin can credit amount to a wallet' do
admin = users(:user_1)
login_as(admin, scope: :user)
2016-07-11 11:40:20 +02:00
w = @vlonchamp.wallet
2016-07-11 16:04:15 +02:00
amount = 10.5
expected_amount = w.amount + amount
put "/api/wallet/#{w.id}/credit",
{
amount: amount
}
assert_equal 200, response.status
assert_equal Mime::JSON, response.content_type
wallet = json_response(response.body)
w.reload
assert_equal w.amount, expected_amount
assert_equal w.amount, wallet[:amount]
end
2016-07-18 18:16:54 +02:00
end