From 285002738fff2c8c206b823d46b6053c810cbbeb Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 1 Oct 2024 13:15:05 +0300 Subject: [PATCH 1/5] added send_with_invalid_recipient test --- menu_traversal_test.go | 31 +++++++++++++++++++++++++++++++ test_data.json | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/menu_traversal_test.go b/menu_traversal_test.go index 9c1e839..7513436 100644 --- a/menu_traversal_test.go +++ b/menu_traversal_test.go @@ -137,3 +137,34 @@ func TestAccountRegistrationInvalidPin(t *testing.T) { } } } + +func TestSendWithInvalidRecipient(t *testing.T) { + en, fn := enginetest.TestEngine("session1234112") + defer fn() + ctx := context.Background() + sessions := testData + for _, session := range sessions { + groups := driver.FilterGroupsByName(session.Groups, "send_with_invalid_recipient") + for _, group := range groups { + for _, step := range group.Steps { + cont, err := en.Exec(ctx, []byte(step.Input)) + if err != nil { + t.Fatalf("Test case '%s' failed at input '%s': %v", group.Name, step.Input, err) + return + } + if !cont { + break + } + w := bytes.NewBuffer(nil) + if _, err := en.Flush(ctx, w); err != nil { + t.Fatalf("Test case '%s' failed during Flush: %v", group.Name, err) + } + + b := w.Bytes() + if !bytes.Equal(b, []byte(step.ExpectedContent)) { + t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) + } + } + } + } +} diff --git a/test_data.json b/test_data.json index 789f064..60a7a97 100644 --- a/test_data.json +++ b/test_data.json @@ -95,6 +95,39 @@ } ] + }, + { + "name": "send_with_invalid_recipient", + "steps": [ + { + "input": "", + "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + }, + { + "input": "1", + "expectedContent": "Enter recipient's phone number:\n0:Back" + }, + { + "input": "000", + "expectedContent": "000 is not registered or invalid, please try again:\n1:retry\n9:Quit" + }, + { + "input": "1", + "expectedContent": "Enter recipient's phone number:\n0:Back" + }, + { + "input": "065656", + "expectedContent": "Maximum amount: 0.003 CELO\nEnter amount:\n0:Back" + }, + { + "input": "0.001", + "expectedContent": "065656 will receive 0.001 from 0xd6CF3C87b0D4aD5978448bBbD6Db9EC6D74D624b\nPlease enter your PIN to confirm:\n0:Back\n9:Quit" + }, + { + "input": "1234", + "expectedContent": "Your request has been sent. 065656 will receive 0.001 from 0xd6CF3C87b0D4aD5978448bBbD6Db9EC6D74D624b." + } + ] } ] } From 3e74c1d939f82fd58b3dbad2ed9245a634d8dc72 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 1 Oct 2024 13:26:47 +0300 Subject: [PATCH 2/5] test with all invalid inputs --- menu_traversal_test.go | 4 ++-- test_data.json | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/menu_traversal_test.go b/menu_traversal_test.go index 7513436..42a5d13 100644 --- a/menu_traversal_test.go +++ b/menu_traversal_test.go @@ -138,13 +138,13 @@ func TestAccountRegistrationInvalidPin(t *testing.T) { } } -func TestSendWithInvalidRecipient(t *testing.T) { +func TestSendWithInvalidInputs(t *testing.T) { en, fn := enginetest.TestEngine("session1234112") defer fn() ctx := context.Background() sessions := testData for _, session := range sessions { - groups := driver.FilterGroupsByName(session.Groups, "send_with_invalid_recipient") + groups := driver.FilterGroupsByName(session.Groups, "send_with_invalid_inputs") for _, group := range groups { for _, step := range group.Steps { cont, err := en.Exec(ctx, []byte(step.Input)) diff --git a/test_data.json b/test_data.json index 60a7a97..f9e3084 100644 --- a/test_data.json +++ b/test_data.json @@ -97,7 +97,7 @@ ] }, { - "name": "send_with_invalid_recipient", + "name": "send_with_invalid_inputs", "steps": [ { "input": "", @@ -119,10 +119,26 @@ "input": "065656", "expectedContent": "Maximum amount: 0.003 CELO\nEnter amount:\n0:Back" }, + { + "input": "0.1", + "expectedContent": "Amount 0.1 is invalid, please try again:\n1:retry\n9:Quit" + }, + { + "input": "1", + "expectedContent": "Maximum amount: 0.003 CELO\nEnter amount:\n0:Back" + }, { "input": "0.001", "expectedContent": "065656 will receive 0.001 from 0xd6CF3C87b0D4aD5978448bBbD6Db9EC6D74D624b\nPlease enter your PIN to confirm:\n0:Back\n9:Quit" }, + { + "input": "1222", + "expectedContent": "Incorrect pin\n1:retry\n9:Quit" + }, + { + "input": "1", + "expectedContent": "065656 will receive 0.001 from 0xd6CF3C87b0D4aD5978448bBbD6Db9EC6D74D624b\nPlease enter your PIN to confirm:\n0:Back\n9:Quit" + }, { "input": "1234", "expectedContent": "Your request has been sent. 065656 will receive 0.001 from 0xd6CF3C87b0D4aD5978448bBbD6Db9EC6D74D624b." From 14218ffb88d0d425a05adb0089a66b51f5680395 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 1 Oct 2024 14:50:45 +0300 Subject: [PATCH 3/5] add a dynamic value --- menu_traversal_test.go | 25 ++++++++++++++++++++++--- test_data.json | 6 +++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/menu_traversal_test.go b/menu_traversal_test.go index 42a5d13..270f360 100644 --- a/menu_traversal_test.go +++ b/menu_traversal_test.go @@ -3,6 +3,7 @@ package main import ( "bytes" "context" + "regexp" "testing" "git.grassecon.net/urdt/ussd/driver" @@ -13,6 +14,17 @@ var ( testData = driver.ReadData() ) +// Extract the public key from the engine response +func extractPublicKey(response []byte) string { + // Regex pattern to match the public key starting with 0x and 40 characters + re := regexp.MustCompile(`0x[a-fA-F0-9]{40}`) + match := re.Find(response) + if match != nil { + return string(match) + } + return "" +} + func TestUserRegistration(t *testing.T) { en, fn := enginetest.TestEngine("session1234112") defer fn() @@ -108,7 +120,7 @@ func TestAccountRegistrationRejectTerms(t *testing.T) { } func TestAccountRegistrationInvalidPin(t *testing.T) { - en, fn := enginetest.TestEngine("session1234112") + en, fn := enginetest.TestEngine("session1234112_c") defer fn() ctx := context.Background() sessions := testData @@ -161,8 +173,15 @@ func TestSendWithInvalidInputs(t *testing.T) { } b := w.Bytes() - if !bytes.Equal(b, []byte(step.ExpectedContent)) { - t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) + + // Extract the dynamic public key from the output + publicKey := extractPublicKey(b) + + // Replace placeholder {public_key} with the actual dynamic public key + expectedContent := bytes.Replace([]byte(step.ExpectedContent), []byte("{public_key}"), []byte(publicKey), -1) + + if !bytes.Equal(b, expectedContent) { + t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expectedContent, b) } } } diff --git a/test_data.json b/test_data.json index f9e3084..44c86c8 100644 --- a/test_data.json +++ b/test_data.json @@ -129,7 +129,7 @@ }, { "input": "0.001", - "expectedContent": "065656 will receive 0.001 from 0xd6CF3C87b0D4aD5978448bBbD6Db9EC6D74D624b\nPlease enter your PIN to confirm:\n0:Back\n9:Quit" + "expectedContent": "065656 will receive 0.001 from {public_key}\nPlease enter your PIN to confirm:\n0:Back\n9:Quit" }, { "input": "1222", @@ -137,11 +137,11 @@ }, { "input": "1", - "expectedContent": "065656 will receive 0.001 from 0xd6CF3C87b0D4aD5978448bBbD6Db9EC6D74D624b\nPlease enter your PIN to confirm:\n0:Back\n9:Quit" + "expectedContent": "065656 will receive 0.001 from {public_key}\nPlease enter your PIN to confirm:\n0:Back\n9:Quit" }, { "input": "1234", - "expectedContent": "Your request has been sent. 065656 will receive 0.001 from 0xd6CF3C87b0D4aD5978448bBbD6Db9EC6D74D624b." + "expectedContent": "Your request has been sent. 065656 will receive 0.001 from {public_key}." } ] } From 48c608ad4d8379bf58657eec78d0990670e29aa1 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 1 Oct 2024 15:36:59 +0300 Subject: [PATCH 4/5] add Help and Quit menu tests --- menu_traversal_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++ test_data.json | 26 ++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/menu_traversal_test.go b/menu_traversal_test.go index 270f360..66f36f6 100644 --- a/menu_traversal_test.go +++ b/menu_traversal_test.go @@ -187,3 +187,65 @@ func TestSendWithInvalidInputs(t *testing.T) { } } } + +func TestMainMenuHelp(t *testing.T) { + en, fn := enginetest.TestEngine("session1234112") + defer fn() + ctx := context.Background() + sessions := testData + for _, session := range sessions { + groups := driver.FilterGroupsByName(session.Groups, "main_menu_help") + for _, group := range groups { + for _, step := range group.Steps { + cont, err := en.Exec(ctx, []byte(step.Input)) + if err != nil { + t.Fatalf("Test case '%s' failed at input '%s': %v", group.Name, step.Input, err) + return + } + if !cont { + break + } + w := bytes.NewBuffer(nil) + if _, err := en.Flush(ctx, w); err != nil { + t.Fatalf("Test case '%s' failed during Flush: %v", group.Name, err) + } + + b := w.Bytes() + if !bytes.Equal(b, []byte(step.ExpectedContent)) { + t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) + } + } + } + } +} + +func TestMainMenuQuit(t *testing.T) { + en, fn := enginetest.TestEngine("session1234112") + defer fn() + ctx := context.Background() + sessions := testData + for _, session := range sessions { + groups := driver.FilterGroupsByName(session.Groups, "main_menu_quit") + for _, group := range groups { + for _, step := range group.Steps { + cont, err := en.Exec(ctx, []byte(step.Input)) + if err != nil { + t.Fatalf("Test case '%s' failed at input '%s': %v", group.Name, step.Input, err) + return + } + if !cont { + break + } + w := bytes.NewBuffer(nil) + if _, err := en.Flush(ctx, w); err != nil { + t.Fatalf("Test case '%s' failed during Flush: %v", group.Name, err) + } + + b := w.Bytes() + if !bytes.Equal(b, []byte(step.ExpectedContent)) { + t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) + } + } + } + } +} diff --git a/test_data.json b/test_data.json index 44c86c8..59e0dcb 100644 --- a/test_data.json +++ b/test_data.json @@ -144,6 +144,32 @@ "expectedContent": "Your request has been sent. 065656 will receive 0.001 from {public_key}." } ] + }, + { + "name": "main_menu_help", + "steps": [ + { + "input": "", + "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + }, + { + "input": "4", + "expectedContent": "For more help,please call: 0757628885" + } + ] + }, + { + "name": "main_menu_quit", + "steps": [ + { + "input": "", + "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + }, + { + "input": "9", + "expectedContent": "Thank you for using Sarafu. Goodbye!" + } + ] } ] } From a0ef57ca1597f7b0aeec1ed6cafacd133fd43623 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 1 Oct 2024 21:43:52 +0300 Subject: [PATCH 5/5] add Change PIN test --- menu_traversal_test.go | 31 +++++++++++++++++++++++++++++++ test_data.json | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/menu_traversal_test.go b/menu_traversal_test.go index 66f36f6..7275afa 100644 --- a/menu_traversal_test.go +++ b/menu_traversal_test.go @@ -249,3 +249,34 @@ func TestMainMenuQuit(t *testing.T) { } } } + +func TestMyAccountChangePin(t *testing.T) { + en, fn := enginetest.TestEngine("session1234112") + defer fn() + ctx := context.Background() + sessions := testData + for _, session := range sessions { + groups := driver.FilterGroupsByName(session.Groups, "my_account_change_pin") + for _, group := range groups { + for _, step := range group.Steps { + cont, err := en.Exec(ctx, []byte(step.Input)) + if err != nil { + t.Fatalf("Test case '%s' failed at input '%s': %v", group.Name, step.Input, err) + return + } + if !cont { + break + } + w := bytes.NewBuffer(nil) + if _, err := en.Flush(ctx, w); err != nil { + t.Fatalf("Test case '%s' failed during Flush: %v", group.Name, err) + } + + b := w.Bytes() + if !bytes.Equal(b, []byte(step.ExpectedContent)) { + t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) + } + } + } + } +} diff --git a/test_data.json b/test_data.json index 59e0dcb..ea6301d 100644 --- a/test_data.json +++ b/test_data.json @@ -170,6 +170,43 @@ "expectedContent": "Thank you for using Sarafu. Goodbye!" } ] + }, + { + "name": "my_account_change_pin", + "steps": [ + { + "input": "", + "expectedContent": "Balance: 0.003 CELO\n\n1:Send\n2:My Vouchers\n3:My Account\n4:Help\n9:Quit" + }, + { + "input": "3", + "expectedContent": "My Account\n1:Profile\n2:Change language\n3:Check balances\n4:Check statement\n5:PIN options\n6:My Address\n0:Back" + }, + { + "input": "5", + "expectedContent": "PIN Management\n1:Change PIN\n2:Reset other's PIN\n3:Guard my PIN\n0:Back" + }, + { + "input": "1", + "expectedContent": "Enter your old PIN\n\n0:Back" + }, + { + "input": "1234", + "expectedContent": "Enter a new four number pin\n\n0:Back" + }, + { + "input": "1234", + "expectedContent": "Confirm your new PIN:\n\n0:Back" + }, + { + "input": "1234", + "expectedContent": "Your PIN change request has been successful\n\n0:Back\n9:Quit" + }, + { + "input": "9", + "expectedContent": "Thank you for using Sarafu. Goodbye!" + } + ] } ] }