From 3664d45f12929618cd02e233272e3bcc854d4405 Mon Sep 17 00:00:00 2001 From: Filip Klosowski Date: Thu, 24 Apr 2025 12:30:28 +0200 Subject: [PATCH 1/2] Added source code for tests --- app/lib/commands/contacts/create/build.liquid | 5 +- .../contact_create_happy_path_test.liquid | 27 ++++++ .../contact_create_negative_path_test.liquid | 85 +++++++++++++++++++ 3 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 app/lib/tests/contact_create_happy_path_test.liquid create mode 100644 app/lib/tests/contact_create_negative_path_test.liquid diff --git a/app/lib/commands/contacts/create/build.liquid b/app/lib/commands/contacts/create/build.liquid index 2f13a76..490d015 100644 --- a/app/lib/commands/contacts/create/build.liquid +++ b/app/lib/commands/contacts/create/build.liquid @@ -1,10 +1,11 @@ {% parse_json contact %} { - "email": {{ object.email | downcase | json }}, + "email": {{ object.email | strip | downcase | json }}, "body": {{ object.body | json }} } {% endparse_json %} {% liquid return contact - %} \ No newline at end of file + %} + \ No newline at end of file diff --git a/app/lib/tests/contact_create_happy_path_test.liquid b/app/lib/tests/contact_create_happy_path_test.liquid new file mode 100644 index 0000000..b9c6c10 --- /dev/null +++ b/app/lib/tests/contact_create_happy_path_test.liquid @@ -0,0 +1,27 @@ +{% comment %} + Test: Valid input - should be considered valid +{% endcomment %} + +{% liquid + assign data = '{ "email": "USER@example.com", "body": "This is a valid message body." }' | parse_json + function contact = 'commands/contacts/create', object: data + + # 1. Contact should be valid + function contract = 'modules/tests/assertions/valid_object', contract: contract, object: contact, field_name: 'contact' + + # 2. Email should be downcased + assign expected_email = 'user@example.com' + assign given_email = contact.email + function contract = 'modules/tests/assertions/equal', contract: contract, expected: expected_email, given: given_email, field_name: 'contact.email' + + # 3. Body should match + assign expected_body = 'This is a valid message body.' + assign given_body = contact.body + function contract = 'modules/tests/assertions/equal', contract: contract, expected: expected_body, given: given_body, field_name: 'contact.body' + + # 4: Assert the contact.valid is true + function contract = 'modules/tests/assertions/true', contract: contract, object: contact, field_name: 'valid' + + # 5: Errors should be blank + function contract = 'modules/tests/assertions/blank', contract: contract, object: contact, field_name: 'errors' +%} diff --git a/app/lib/tests/contact_create_negative_path_test.liquid b/app/lib/tests/contact_create_negative_path_test.liquid new file mode 100644 index 0000000..c04a4ca --- /dev/null +++ b/app/lib/tests/contact_create_negative_path_test.liquid @@ -0,0 +1,85 @@ +{% comment %} + Test: Invalid email - should be invalid +{% endcomment %} +{% liquid + assign data = '{ "email": "invalid-email", "body": "This is a valid message body." }' | parse_json + function contact = 'commands/contacts/create', object: data + + function contract = 'modules/tests/assertions/not_valid_object', contract: contract, object: contact, field_name: 'contact' + function contract = 'modules/tests/assertions/presence', contract: contract, object: contact.errors, field_name: 'email' +%} + +{% comment %} + Test: Too short body - should be invalid +{% endcomment %} +{% liquid + assign data = '{ "email": "user@example.com", "body": "Short" }' | parse_json + function contact = 'commands/contacts/create', object: data + + function contract = 'modules/tests/assertions/not_valid_object', contract: contract, object: contact, field_name: 'contact' + function contract = 'modules/tests/assertions/presence', contract: contract, object: contact.errors, field_name: 'body' +%} + +{% comment %} + Test: Too long body – should be invalid +{% endcomment %} + +{% parse_json data %} +{ + "email": "user@example.com", + "body": "{{ 201 | random_string }}" +} +{% endparse_json %} + +{% liquid + function contact = 'commands/contacts/create', object: data + + function contract = 'modules/tests/assertions/not_valid_object', contract: contract, object: contact, field_name: 'contact' + function contract = 'modules/tests/assertions/presence', contract: contract, object: contact.errors, field_name: 'body' +%} + +{% comment %} + Test: Blank email and body - should be invalid +{% endcomment %} +{% liquid + assign data = '{ "email": "", "body": "" }' | parse_json + function contact = 'commands/contacts/create', object: data + + function contract = 'modules/tests/assertions/not_valid_object', contract: contract, object: contact, field_name: 'contact' + function contract = 'modules/tests/assertions/presence', contract: contract, object: contact.errors, field_name: 'email' + function contract = 'modules/tests/assertions/presence', contract: contract, object: contact.errors, field_name: 'body' +%} + +{% comment %} + Test: Body is missing (null) – should fail validation +{% endcomment %} +{% liquid + assign data = '{ "email": "user@example.com" }' | parse_json + function contact = 'commands/contacts/create', object: data + + function contract = 'modules/tests/assertions/not_valid_object', contract: contract, object: contact, field_name: 'contact' + function contract = 'modules/tests/assertions/presence', contract: contract, object: contact.errors, field_name: 'body' +%} + +{% comment %} + Test: Email missing from object – should fail validation +{% endcomment %} +{% liquid + assign data = '{ "body": "This is valid" }' | parse_json + function contact = 'commands/contacts/create', object: data + + function contract = 'modules/tests/assertions/not_valid_object', contract: contract, object: contact, field_name: 'contact' + function contract = 'modules/tests/assertions/presence', contract: contract, object: contact.errors, field_name: 'email' +%} + +{% comment %} + Test: Completely empty object – both fields invalid +{% endcomment %} +{% liquid + assign data = '{}' | parse_json + function contact = 'commands/contacts/create', object: data + + function contract = 'modules/tests/assertions/not_valid_object', contract: contract, object: contact, field_name: 'contact' + function contract = 'modules/tests/assertions/presence', contract: contract, object: contact.errors, field_name: 'email' + function contract = 'modules/tests/assertions/presence', contract: contract, object: contact.errors, field_name: 'body' +%} From 643a52a9fb887f65e710c5eae5c624bff5ab1bca Mon Sep 17 00:00:00 2001 From: Filip Klosowski Date: Thu, 24 Apr 2025 12:32:23 +0200 Subject: [PATCH 2/2] Added missing test --- .../tests/contact_create_negative_path_test.liquid | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/lib/tests/contact_create_negative_path_test.liquid b/app/lib/tests/contact_create_negative_path_test.liquid index c04a4ca..b3bdd5f 100644 --- a/app/lib/tests/contact_create_negative_path_test.liquid +++ b/app/lib/tests/contact_create_negative_path_test.liquid @@ -83,3 +83,15 @@ function contract = 'modules/tests/assertions/presence', contract: contract, object: contact.errors, field_name: 'email' function contract = 'modules/tests/assertions/presence', contract: contract, object: contact.errors, field_name: 'body' %} + +{% comment %} + Test: Email with upper case letters and leading/trailing spaces +{% endcomment %} +{% liquid + assign data = '{ "email": " User@examplE.com ", "body": "Nice body here" }' | parse_json + function contact = 'commands/contacts/create/build', object: data + + if contact.email != 'user@example.com' + function contract = 'modules/tests/helpers/register_error', contract: contract, field_name: 'email', message: 'Email was not trimmed or downcased' + endif +%}