From 52f6ad6fe19149a351fba673aa378508cd7c0558 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 16 Dec 2013 01:04:25 +0000 Subject: [PATCH] Added validation tests for "/account set" and "/account set jid" --- tests/config/mock_accounts.c | 16 +++- tests/test_cmd_account.c | 146 +++++++++++++++++++++++++++++++++++ tests/test_cmd_account.h | 8 ++ tests/testsuite.c | 96 ++++++++++++----------- 4 files changed, 220 insertions(+), 46 deletions(-) diff --git a/tests/config/mock_accounts.c b/tests/config/mock_accounts.c index b8cc9596..8cd8b93e 100644 --- a/tests/config/mock_accounts.c +++ b/tests/config/mock_accounts.c @@ -85,12 +85,24 @@ gboolean accounts_rename(const char * const account_name, gboolean accounts_account_exists(const char * const account_name) { + check_expected(account_name); return (gboolean)mock(); } -void accounts_set_jid(const char * const account_name, const char * const value) {} +void accounts_set_jid(const char * const account_name, const char * const value) +{ + check_expected(account_name); + check_expected(value); +} + void accounts_set_server(const char * const account_name, const char * const value) {} -void accounts_set_resource(const char * const account_name, const char * const value) {} + +void accounts_set_resource(const char * const account_name, const char * const value) +{ + check_expected(account_name); + check_expected(value); +} + void accounts_set_password(const char * const account_name, const char * const value) {} void accounts_set_muc_service(const char * const account_name, const char * const value) {} void accounts_set_muc_nick(const char * const account_name, const char * const value) {} diff --git a/tests/test_cmd_account.c b/tests/test_cmd_account.c index 95924f59..2e43bca6 100644 --- a/tests/test_cmd_account.c +++ b/tests/test_cmd_account.c @@ -374,3 +374,149 @@ void cmd_account_rename_shows_message_when_not_renamed(void **state) free(help); } + +void cmd_account_set_shows_usage_when_no_args(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", NULL }; + + expect_string(cons_show, output, "Usage: some usage"); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_set_shows_usage_when_one_arg(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", "a_account", NULL }; + + expect_string(cons_show, output, "Usage: some usage"); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_set_shows_usage_when_two_args(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", "a_account", "a_property", NULL }; + + expect_string(cons_show, output, "Usage: some usage"); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_set_checks_account_exists(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", "a_account", "a_property", "a_value", NULL }; + + expect_string(accounts_account_exists, account_name, "a_account"); + will_return(accounts_account_exists, FALSE); + + expect_any_count(cons_show, output, 2); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_set_shows_message_when_account_doesnt_exist(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", "a_account", "a_property", "a_value", NULL }; + + expect_any(accounts_account_exists, account_name); + will_return(accounts_account_exists, FALSE); + + expect_string(cons_show, output, "Account a_account doesn't exist"); + expect_string(cons_show, output, ""); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_set_jid_shows_message_for_malformed_jid(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", "a_account", "jid", "@malformed", NULL }; + + expect_any(accounts_account_exists, account_name); + will_return(accounts_account_exists, TRUE); + + expect_string(cons_show, output, "Malformed jid: @malformed"); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_set_jid_sets_barejid(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", "a_account", "jid", "a_local@a_domain/a_resource", NULL }; + + expect_any(accounts_account_exists, account_name); + will_return(accounts_account_exists, TRUE); + + expect_string(accounts_set_jid, account_name, "a_account"); + expect_string(accounts_set_jid, value, "a_local@a_domain"); + + expect_string(cons_show, output, "Updated jid for account a_account: a_local@a_domain"); + + expect_any(accounts_set_resource, account_name); + expect_any(accounts_set_resource, value); + + expect_any(cons_show, output); + expect_string(cons_show, output, ""); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_set_jid_sets_resource(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", "a_account", "jid", "a_local@a_domain/a_resource", NULL }; + + expect_any(accounts_account_exists, account_name); + will_return(accounts_account_exists, TRUE); + + expect_any(accounts_set_jid, account_name); + expect_any(accounts_set_jid, value); + + expect_any(cons_show, output); + + expect_string(accounts_set_resource, account_name, "a_account"); + expect_string(accounts_set_resource, value, "a_resource"); + + expect_string(cons_show, output, "Updated resource for account a_account: a_resource"); + expect_string(cons_show, output, ""); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} diff --git a/tests/test_cmd_account.h b/tests/test_cmd_account.h index ca72af36..62783609 100644 --- a/tests/test_cmd_account.h +++ b/tests/test_cmd_account.h @@ -20,3 +20,11 @@ void cmd_account_rename_shows_usage_when_one_arg(void **state); void cmd_account_rename_renames_account(void **state); void cmd_account_rename_shows_message_when_renamed(void **state); void cmd_account_rename_shows_message_when_not_renamed(void **state); +void cmd_account_set_shows_usage_when_no_args(void **state); +void cmd_account_set_shows_usage_when_one_arg(void **state); +void cmd_account_set_shows_usage_when_two_args(void **state); +void cmd_account_set_checks_account_exists(void **state); +void cmd_account_set_shows_message_when_account_doesnt_exist(void **state); +void cmd_account_set_jid_shows_message_for_malformed_jid(void **state); +void cmd_account_set_jid_sets_barejid(void **state); +void cmd_account_set_jid_sets_resource(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index bba13a7e..44c014df 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -15,50 +15,6 @@ int main(int argc, char* argv[]) { const UnitTest tests[] = { - unit_test(cmd_connect_shows_message_when_disconnecting), - unit_test(cmd_connect_shows_message_when_connecting), - unit_test(cmd_connect_shows_message_when_connected), - unit_test(cmd_connect_shows_message_when_undefined), - unit_test(cmd_connect_when_no_account), - unit_test(cmd_connect_with_altdomain_when_provided), - unit_test(cmd_connect_fail_message), - unit_test(cmd_connect_lowercases_argument), - unit_test(cmd_connect_asks_password_when_not_in_account), - unit_test(cmd_connect_shows_message_when_connecting_with_account), - unit_test(cmd_connect_connects_with_account), - unit_test(cmd_connect_frees_account_after_connecting), - - unit_test(cmd_account_shows_usage_when_not_connected_and_no_args), - unit_test(cmd_account_shows_account_when_connected_and_no_args), - unit_test(cmd_account_list_shows_accounts), - unit_test(cmd_account_show_shows_usage_when_no_arg), - unit_test(cmd_account_show_shows_message_when_account_does_not_exist), - unit_test(cmd_account_show_shows_message_when_account_exists), - unit_test(cmd_account_add_shows_usage_when_no_arg), - unit_test(cmd_account_add_adds_account), - unit_test(cmd_account_add_shows_message), - unit_test(cmd_account_enable_shows_usage_when_no_arg), - unit_test(cmd_account_enable_enables_account), - unit_test(cmd_account_enable_shows_message_when_enabled), - unit_test(cmd_account_enable_shows_message_when_account_doesnt_exist), - unit_test(cmd_account_disable_shows_usage_when_no_arg), - unit_test(cmd_account_disable_disables_account), - unit_test(cmd_account_disable_shows_message_when_disabled), - unit_test(cmd_account_disable_shows_message_when_account_doesnt_exist), - unit_test(cmd_account_rename_shows_usage_when_no_args), - unit_test(cmd_account_rename_shows_usage_when_one_arg), - unit_test(cmd_account_rename_renames_account), - unit_test(cmd_account_rename_shows_message_when_renamed), - unit_test(cmd_account_rename_shows_message_when_not_renamed), - - unit_test(cmd_rooms_shows_message_when_disconnected), - unit_test(cmd_rooms_shows_message_when_disconnecting), - unit_test(cmd_rooms_shows_message_when_connecting), - unit_test(cmd_rooms_shows_message_when_started), - unit_test(cmd_rooms_shows_message_when_undefined), - unit_test(cmd_rooms_uses_account_default_when_no_arg), - unit_test(cmd_rooms_arg_used_when_passed), - unit_test(replace_one_substr), unit_test(replace_one_substr_beginning), unit_test(replace_one_substr_end), @@ -203,6 +159,58 @@ int main(int argc, char* argv[]) { unit_test(find_twice_returns_second_when_two_match), unit_test(find_five_times_finds_fifth), unit_test(find_twice_returns_first_when_two_match_and_reset), + + unit_test(cmd_connect_shows_message_when_disconnecting), + unit_test(cmd_connect_shows_message_when_connecting), + unit_test(cmd_connect_shows_message_when_connected), + unit_test(cmd_connect_shows_message_when_undefined), + unit_test(cmd_connect_when_no_account), + unit_test(cmd_connect_with_altdomain_when_provided), + unit_test(cmd_connect_fail_message), + unit_test(cmd_connect_lowercases_argument), + unit_test(cmd_connect_asks_password_when_not_in_account), + unit_test(cmd_connect_shows_message_when_connecting_with_account), + unit_test(cmd_connect_connects_with_account), + unit_test(cmd_connect_frees_account_after_connecting), + + unit_test(cmd_rooms_shows_message_when_disconnected), + unit_test(cmd_rooms_shows_message_when_disconnecting), + unit_test(cmd_rooms_shows_message_when_connecting), + unit_test(cmd_rooms_shows_message_when_started), + unit_test(cmd_rooms_shows_message_when_undefined), + unit_test(cmd_rooms_uses_account_default_when_no_arg), + unit_test(cmd_rooms_arg_used_when_passed), + + unit_test(cmd_account_shows_usage_when_not_connected_and_no_args), + unit_test(cmd_account_shows_account_when_connected_and_no_args), + unit_test(cmd_account_list_shows_accounts), + unit_test(cmd_account_show_shows_usage_when_no_arg), + unit_test(cmd_account_show_shows_message_when_account_does_not_exist), + unit_test(cmd_account_show_shows_message_when_account_exists), + unit_test(cmd_account_add_shows_usage_when_no_arg), + unit_test(cmd_account_add_adds_account), + unit_test(cmd_account_add_shows_message), + unit_test(cmd_account_enable_shows_usage_when_no_arg), + unit_test(cmd_account_enable_enables_account), + unit_test(cmd_account_enable_shows_message_when_enabled), + unit_test(cmd_account_enable_shows_message_when_account_doesnt_exist), + unit_test(cmd_account_disable_shows_usage_when_no_arg), + unit_test(cmd_account_disable_disables_account), + unit_test(cmd_account_disable_shows_message_when_disabled), + unit_test(cmd_account_disable_shows_message_when_account_doesnt_exist), + unit_test(cmd_account_rename_shows_usage_when_no_args), + unit_test(cmd_account_rename_shows_usage_when_one_arg), + unit_test(cmd_account_rename_renames_account), + unit_test(cmd_account_rename_shows_message_when_renamed), + unit_test(cmd_account_rename_shows_message_when_not_renamed), + unit_test(cmd_account_set_shows_usage_when_no_args), + unit_test(cmd_account_set_shows_usage_when_one_arg), + unit_test(cmd_account_set_shows_usage_when_two_args), + unit_test(cmd_account_set_checks_account_exists), + unit_test(cmd_account_set_shows_message_when_account_doesnt_exist), + unit_test(cmd_account_set_jid_shows_message_for_malformed_jid), + unit_test(cmd_account_set_jid_sets_barejid), + unit_test(cmd_account_set_jid_sets_resource), }; return run_tests(tests); }