Skip to content

Commit

Permalink
DigixGlobal#70 resend_transaction_mutation_test refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
bshevchenko committed Sep 5, 2019
1 parent 84cb85a commit 644919e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 74 deletions.
2 changes: 1 addition & 1 deletion app/models/watching_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def resend(user, watching_transaction, attrs)
unless user.id == watching_transaction.user.id
return [:unauthorized_action, nil]
end
unless transaction_object.fetch('nonce') == watching_transaction.transaction_object.fetch('nonce')
unless transaction_object.fetch('nonce') == watching_transaction.transaction_object['nonce']
return [:invalid_nonce, nil]
end

Expand Down
2 changes: 1 addition & 1 deletion test/factories/watching_transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'faker'

FactoryBot.define do
sequence(:transaction_object) { |_| Faker::TvShows::SiliconValley.invention }
sequence(:transaction_object) { |_| { nonce: 1 } }
sequence(:signed_transaction) { |_| SecureRandom.hex(32) }

factory :watching_transaction, class: 'WatchingTransaction' do
Expand Down
87 changes: 15 additions & 72 deletions test/graphql/resend_transaction_mutation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,6 @@

class ResendTransactionMutationTest < ActiveSupport::TestCase
QUERY = <<~EOS
mutation($transactionObject: JSON!, $signedTransaction: String!) {
watchTransaction(input: { transactionObject: $transactionObject, signedTransaction: $signedTransaction }) {
watchedTransaction {
id
user {
address
}
transactionObject
}
errors {
field
message
}
}
}
EOS

QUERY2 = <<~EOS
mutation($id: String!, $transactionObject: JSON!, $signedTransaction: String!) {
resendTransaction(input: { id: $id, transactionObject: $transactionObject, signedTransaction: $signedTransaction }) {
watchedTransaction {
Expand All @@ -40,56 +22,34 @@ class ResendTransactionMutationTest < ActiveSupport::TestCase
EOS

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

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

assert_nil tx_result['errors'],
'should work and have no errors'
assert_empty tx_result['data']['watchTransaction']['errors'],
'should have no errors'

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

transaction_object = {
'nonce' => 1,
'transactionHash' => generate(:txhash)
}
attrs = {
'id' => data['id'],
'transactionObject' => JSON.generate(transaction_object),
'signedTransaction' => generate(:signed_transaction)
}

tx2_result = DaoServerSchema.execute(
QUERY2,
context: { current_user: user },
variables: attrs
)

assert_nil tx2_result['errors'],
'should work and have no errors'
assert_empty tx2_result['data']['resendTransaction']['errors'],
assert_empty tx_result['data']['resendTransaction']['errors'],
'should have no errors'

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

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

assert_equal data['transactionObject']['nonce'], data2['transactionObject']['nonce'],
assert_equal data['transactionObject']['nonce'], data['transactionObject']['nonce'],
'nonce should be the same'
end

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

user = create(:user)
old = create(:watching_transaction)
transaction_object = {
'nonce' => 1,
'transactionHash' => generate(:txhash)
}
attrs = {
'transactionObject' => JSON.generate(transaction_object),
'signedTransaction' => generate(:signed_transaction)
}

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

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

transaction_object = {
'nonce' => 1,
'nonce' => old.transaction_object.fetch('nonce'),
'transactionHash' => generate(:txhash)
}
attrs = {
Expand All @@ -132,8 +75,8 @@ class ResendTransactionMutationTest < ActiveSupport::TestCase
}

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

Expand All @@ -144,12 +87,12 @@ class ResendTransactionMutationTest < ActiveSupport::TestCase
'nonce' => 2,
'transactionHash' => generate(:txhash)
}
attrs['id'] = data['id']
attrs['id'] = old.id
attrs['transactionObject'] = JSON.generate(transaction_object)

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

Expand Down

0 comments on commit 644919e

Please sign in to comment.