Skip to content

Commit

Permalink
DigixGlobal#70 watch & resend mutations tests refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
bshevchenko committed Sep 9, 2019
1 parent a0f9970 commit 690182b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 49 deletions.
2 changes: 1 addition & 1 deletion app/graphql/mutations/resend_transaction_mutation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Mutations
class ResendTransactionMutation < Types::Base::BaseMutation
description 'Given an old transaction, resend it with new parameters or gas prices'

argument :id, String,
argument :id, ID,
required: true,
description: 'ID of the transaction to be resent'
argument :transaction_object, Types::Scalar::JSONObject,
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1694,7 +1694,7 @@ input ResendTransactionMutationInput {
"""
ID of the transaction to be resent
"""
id: String!
id: ID!

"""
Signed transaction in HEX format
Expand Down
22 changes: 20 additions & 2 deletions test/factories/watching_transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,32 @@

require 'faker'

static_nonce = Random.rand(1001..2000)

FactoryBot.define do
sequence(:transaction_object) { |_| { nonce: 1 } }
sequence(:transaction_object) do |_|
{ nonce: Random.rand(1..1000), transactionHash: Faker::Blockchain::Ethereum.address }
end
sequence(:fixed_transaction_object) do |_|
{ nonce: static_nonce, transactionHash: Faker::Blockchain::Ethereum.address }
end
sequence(:signed_transaction) { |_| SecureRandom.hex(32) }
sequence(:id) { |_| SecureRandom.hex(32) }

factory :watching_transaction, class: 'WatchingTransaction' do
txhash { generate(:txhash) }
transaction_object { generate(:transaction_object) }
transaction_object { generate(:fixed_transaction_object) }
signed_transaction { generate(:signed_transaction) }
association :user, factory: :user
end

factory :watch_transaction, class: 'Hash' do
transactionObject { JSON.generate(generate(:transaction_object)) }
signedTransaction { generate(:signed_transaction) }

factory :watch_transaction_resend do
id { generate(:id) }
transactionObject { JSON.generate(generate(:fixed_transaction_object)) }
end
end
end
46 changes: 10 additions & 36 deletions test/graphql/resend_transaction_mutation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class ResendTransactionMutationTest < ActiveSupport::TestCase
QUERY = <<~EOS
mutation($id: String!, $transactionObject: JSONObject!, $signedTransaction: String!) {
mutation($id: ID!, $transactionObject: JSONObject!, $signedTransaction: String!) {
resendTransaction(input: { id: $id, transactionObject: $transactionObject, signedTransaction: $signedTransaction }) {
watchedTransaction {
id
Expand All @@ -22,20 +22,12 @@ class ResendTransactionMutationTest < ActiveSupport::TestCase
EOS

test 'resend transaction mutation should work' do
old = create(:watching_transaction)
transaction_object = {
'nonce' => old.transaction_object.fetch('nonce'),
'transactionHash' => generate(:txhash)
}
attrs = {
'id' => old.id,
'transactionObject' => JSON.generate(transaction_object),
'signedTransaction' => generate(:signed_transaction)
}
old_transaction = create(:watching_transaction)
attrs = attributes_for(:watch_transaction_resend, id: old_transaction.id)

tx_result = DaoServerSchema.execute(
QUERY,
context: { current_user: old.user },
context: { current_user: old_transaction.user },
variables: attrs
)

Expand All @@ -46,10 +38,7 @@ class ResendTransactionMutationTest < ActiveSupport::TestCase

data = tx_result['data']['resendTransaction']['watchedTransaction']

assert_equal transaction_object, data['transactionObject'],
'transactionObject should be the same'

assert_equal data['transactionObject']['nonce'], data['transactionObject']['nonce'],
assert_equal JSON.parse(attrs[:transactionObject])['nonce'], data['transactionObject']['nonce'],
'nonce should be the same'
end

Expand All @@ -63,37 +52,22 @@ class ResendTransactionMutationTest < ActiveSupport::TestCase
assert_not_empty unauthorized_result['errors'],
'should fail without a user'

old = create(:watching_transaction)
transaction_object = {
'nonce' => old.transaction_object.fetch('nonce'),
'transactionHash' => generate(:txhash)
}
attrs = {
'id' => 'abc',
'transactionObject' => JSON.generate(transaction_object),
'signedTransaction' => generate(:signed_transaction)
}
old_transaction = create(:watching_transaction)
attrs = attributes_for(:watch_transaction_resend)

invalid_group_result = DaoServerSchema.execute(
QUERY,
context: { current_user: old.user },
context: { current_user: old_transaction.user },
variables: attrs
)

assert_not_empty invalid_group_result['data']['resendTransaction']['errors'],
'should fail with invalid id'

transaction_object = {
'nonce' => 2,
'transactionHash' => generate(:txhash)
}
attrs['id'] = old.id
attrs['transactionObject'] = JSON.generate(transaction_object)

invalid_nonce_result = DaoServerSchema.execute(
QUERY,
context: { current_user: old.user },
variables: attrs
context: { current_user: old_transaction.user },
variables: attributes_for(:watch_transaction, id: old_transaction.id)
)

assert_not_empty invalid_nonce_result['data']['resendTransaction']['errors'],
Expand Down
11 changes: 2 additions & 9 deletions test/graphql/watch_transaction_mutation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ class WatchTransactionMutationTest < ActiveSupport::TestCase

test 'watch transaction mutation should work' do
user = create(:user)
transaction_object = {
'nonce' => 1,
'transactionHash' => generate(:txhash)
}
attrs = {
'transactionObject' => JSON.generate(transaction_object),
'signedTransaction' => generate(:signed_transaction)
}
attrs = attributes_for(:watch_transaction)

tx_result = DaoServerSchema.execute(
QUERY,
Expand All @@ -45,7 +38,7 @@ class WatchTransactionMutationTest < ActiveSupport::TestCase

data = tx_result['data']['watchTransaction']['watchedTransaction']

assert_equal transaction_object, data['transactionObject'],
assert_equal JSON.parse(attrs[:transactionObject]), data['transactionObject'],
'transactionObject should be the same'
end

Expand Down

0 comments on commit 690182b

Please sign in to comment.