Skip to content

Commit

Permalink
switch rc_api_init_login_request to login2 API (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras authored Oct 7, 2023
1 parent 9f33b3d commit e3c3621
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/rapi/rc_api_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int rc_api_init_login_request(rc_api_request_t* request, const rc_api_login_requ
return RC_INVALID_STATE;

rc_url_builder_init(&builder, &request->buffer, 48);
rc_url_builder_append_str_param(&builder, "r", "login");
rc_url_builder_append_str_param(&builder, "r", "login2");
rc_url_builder_append_str_param(&builder, "u", api_params->username);

if (api_params->password && api_params->password[0])
Expand Down
140 changes: 131 additions & 9 deletions test/rapi/test_rc_api_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static void test_init_login_request_password()

ASSERT_NUM_EQUALS(rc_api_init_login_request(&request, &login_request), RC_OK);
ASSERT_STR_EQUALS(request.url, DOREQUEST_URL);
ASSERT_STR_EQUALS(request.post_data, "r=login&u=Username&p=Pa%24%24w0rd%21");
ASSERT_STR_EQUALS(request.post_data, "r=login2&u=Username&p=Pa%24%24w0rd%21");
ASSERT_STR_EQUALS(request.content_type, RC_CONTENT_TYPE_URLENCODED);

rc_api_destroy_request(&request);
Expand All @@ -129,7 +129,7 @@ static void test_init_login_request_password_long()
int i;

/* this generates a password that's 830 characters long */
ptr = password_start = buffer + snprintf(buffer, sizeof(buffer), "r=login&u=ThisUsernameIsAlsoReallyLongAtRoughlyFiftyCharacters&p=");
ptr = password_start = buffer + snprintf(buffer, sizeof(buffer), "r=login2&u=ThisUsernameIsAlsoReallyLongAtRoughlyFiftyCharacters&p=");
for (i = 0; i < 30; i++)
ptr += snprintf(ptr, sizeof(buffer) - (ptr - buffer), "%dABCDEFGHIJKLMNOPQRSTUVWXYZ", i);

Expand All @@ -156,7 +156,7 @@ static void test_init_login_request_token()

ASSERT_NUM_EQUALS(rc_api_init_login_request(&request, &login_request), RC_OK);
ASSERT_STR_EQUALS(request.url, DOREQUEST_URL);
ASSERT_STR_EQUALS(request.post_data, "r=login&u=Username&t=ABCDEFGHIJKLMNOP");
ASSERT_STR_EQUALS(request.post_data, "r=login2&u=Username&t=ABCDEFGHIJKLMNOP");
ASSERT_STR_EQUALS(request.content_type, RC_CONTENT_TYPE_URLENCODED);

rc_api_destroy_request(&request);
Expand All @@ -174,7 +174,7 @@ static void test_init_login_request_password_and_token()

ASSERT_NUM_EQUALS(rc_api_init_login_request(&request, &login_request), RC_OK);
ASSERT_STR_EQUALS(request.url, DOREQUEST_URL);
ASSERT_STR_EQUALS(request.post_data, "r=login&u=Username&p=Pa%24%24w0rd%21");
ASSERT_STR_EQUALS(request.post_data, "r=login2&u=Username&p=Pa%24%24w0rd%21");
ASSERT_STR_EQUALS(request.content_type, RC_CONTENT_TYPE_URLENCODED);

rc_api_destroy_request(&request);
Expand Down Expand Up @@ -205,7 +205,7 @@ static void test_init_login_request_alternate_host()
rc_api_set_host("localhost");
ASSERT_NUM_EQUALS(rc_api_init_login_request(&request, &login_request), RC_OK);
ASSERT_STR_EQUALS(request.url, "http://localhost/dorequest.php");
ASSERT_STR_EQUALS(request.post_data, "r=login&u=Username&p=Pa%24%24w0rd%21");
ASSERT_STR_EQUALS(request.post_data, "r=login2&u=Username&p=Pa%24%24w0rd%21");
ASSERT_STR_EQUALS(request.content_type, RC_CONTENT_TYPE_URLENCODED);

rc_api_set_host(NULL);
Expand All @@ -215,6 +215,7 @@ static void test_init_login_request_alternate_host()
static void test_process_login_response_success()
{
rc_api_login_response_t login_response;
rc_api_server_response_t response_obj;
const char* server_response = "{\"Success\":true,\"User\":\"USER\",\"Token\":\"ApiTOKEN\",\"Score\":1234,\"SoftcoreScore\":789,\"Messages\":2}";

memset(&login_response, 0, sizeof(login_response));
Expand All @@ -230,6 +231,25 @@ static void test_process_login_response_success()
ASSERT_STR_EQUALS(login_response.display_name, "USER");

rc_api_destroy_login_response(&login_response);

memset(&response_obj, 0, sizeof(response_obj));
response_obj.body = server_response;
response_obj.body_length = rc_json_get_object_string_length(server_response);
response_obj.http_status_code = 200;

memset(&login_response, 0, sizeof(login_response));

ASSERT_NUM_EQUALS(rc_api_process_login_server_response(&login_response, &response_obj), RC_OK);
ASSERT_NUM_EQUALS(login_response.response.succeeded, 1);
ASSERT_PTR_NULL(login_response.response.error_message);
ASSERT_STR_EQUALS(login_response.username, "USER");
ASSERT_STR_EQUALS(login_response.api_token, "ApiTOKEN");
ASSERT_NUM_EQUALS(login_response.score, 1234);
ASSERT_NUM_EQUALS(login_response.score_softcore, 789);
ASSERT_NUM_EQUALS(login_response.num_unread_messages, 2);
ASSERT_STR_EQUALS(login_response.display_name, "USER");

rc_api_destroy_login_response(&login_response);
}

static void test_process_login_response_unique_display_name()
Expand All @@ -252,14 +272,34 @@ static void test_process_login_response_unique_display_name()
rc_api_destroy_login_response(&login_response);
}

static void test_process_login_response_error()
static void test_process_login_response_invalid_credentials()
{
rc_api_login_response_t login_response;
const char* server_response = "{\"Success\":false,\"Error\":\"Invalid User/Password combination. Please try again\"}";
rc_api_server_response_t response_obj;
const char* server_response = "{\"Success\":false,\"Error\":\"Invalid User/Password combination. Please try again\", \"Code\":\"invalid_credentials\"}";

memset(&login_response, 0, sizeof(login_response));

ASSERT_NUM_EQUALS(rc_api_process_login_response(&login_response, server_response), RC_OK);
ASSERT_NUM_EQUALS(rc_api_process_login_response(&login_response, server_response), RC_INVALID_CREDENTIALS);
ASSERT_NUM_EQUALS(login_response.response.succeeded, 0);
ASSERT_STR_EQUALS(login_response.response.error_message, "Invalid User/Password combination. Please try again");
ASSERT_PTR_NULL(login_response.username);
ASSERT_PTR_NULL(login_response.api_token);
ASSERT_NUM_EQUALS(login_response.score, 0);
ASSERT_NUM_EQUALS(login_response.score_softcore, 0);
ASSERT_NUM_EQUALS(login_response.num_unread_messages, 0);
ASSERT_PTR_NULL(login_response.display_name);

rc_api_destroy_login_response(&login_response);

memset(&response_obj, 0, sizeof(response_obj));
response_obj.body = server_response;
response_obj.body_length = rc_json_get_object_string_length(server_response);
response_obj.http_status_code = 401;

memset(&login_response, 0, sizeof(login_response));

ASSERT_NUM_EQUALS(rc_api_process_login_server_response(&login_response, &response_obj), RC_INVALID_CREDENTIALS);
ASSERT_NUM_EQUALS(login_response.response.succeeded, 0);
ASSERT_STR_EQUALS(login_response.response.error_message, "Invalid User/Password combination. Please try again");
ASSERT_PTR_NULL(login_response.username);
Expand All @@ -272,6 +312,86 @@ static void test_process_login_response_error()
rc_api_destroy_login_response(&login_response);
}

static void test_process_login_response_access_denied()
{
rc_api_login_response_t login_response;
rc_api_server_response_t response_obj;
const char* server_response = "{\"Success\":false,\"Error\":\"Access denied.\",\"Code\":\"access_denied\"}";

memset(&login_response, 0, sizeof(login_response));

ASSERT_NUM_EQUALS(rc_api_process_login_response(&login_response, server_response), RC_ACCESS_DENIED);
ASSERT_NUM_EQUALS(login_response.response.succeeded, 0);
ASSERT_STR_EQUALS(login_response.response.error_message, "Access denied.");
ASSERT_PTR_NULL(login_response.username);
ASSERT_PTR_NULL(login_response.api_token);
ASSERT_NUM_EQUALS(login_response.score, 0);
ASSERT_NUM_EQUALS(login_response.score_softcore, 0);
ASSERT_NUM_EQUALS(login_response.num_unread_messages, 0);
ASSERT_PTR_NULL(login_response.display_name);

rc_api_destroy_login_response(&login_response);

memset(&response_obj, 0, sizeof(response_obj));
response_obj.body = server_response;
response_obj.body_length = rc_json_get_object_string_length(server_response);
response_obj.http_status_code = 403;

memset(&login_response, 0, sizeof(login_response));

ASSERT_NUM_EQUALS(rc_api_process_login_server_response(&login_response, &response_obj), RC_ACCESS_DENIED);
ASSERT_NUM_EQUALS(login_response.response.succeeded, 0);
ASSERT_STR_EQUALS(login_response.response.error_message, "Access denied.");
ASSERT_PTR_NULL(login_response.username);
ASSERT_PTR_NULL(login_response.api_token);
ASSERT_NUM_EQUALS(login_response.score, 0);
ASSERT_NUM_EQUALS(login_response.score_softcore, 0);
ASSERT_NUM_EQUALS(login_response.num_unread_messages, 0);
ASSERT_PTR_NULL(login_response.display_name);

rc_api_destroy_login_response(&login_response);
}

static void test_process_login_response_expired_token()
{
rc_api_login_response_t login_response;
rc_api_server_response_t response_obj;
const char* server_response = "{\"Success\":false,\"Error\":\"The access token has expired. Please log in again.\",\"Code\":\"expired_token\"}";

memset(&login_response, 0, sizeof(login_response));

ASSERT_NUM_EQUALS(rc_api_process_login_response(&login_response, server_response), RC_EXPIRED_TOKEN);
ASSERT_NUM_EQUALS(login_response.response.succeeded, 0);
ASSERT_STR_EQUALS(login_response.response.error_message, "The access token has expired. Please log in again.");
ASSERT_PTR_NULL(login_response.username);
ASSERT_PTR_NULL(login_response.api_token);
ASSERT_NUM_EQUALS(login_response.score, 0);
ASSERT_NUM_EQUALS(login_response.score_softcore, 0);
ASSERT_NUM_EQUALS(login_response.num_unread_messages, 0);
ASSERT_PTR_NULL(login_response.display_name);

rc_api_destroy_login_response(&login_response);

memset(&response_obj, 0, sizeof(response_obj));
response_obj.body = server_response;
response_obj.body_length = rc_json_get_object_string_length(server_response);
response_obj.http_status_code = 401;

memset(&login_response, 0, sizeof(login_response));

ASSERT_NUM_EQUALS(rc_api_process_login_server_response(&login_response, &response_obj), RC_EXPIRED_TOKEN);
ASSERT_NUM_EQUALS(login_response.response.succeeded, 0);
ASSERT_STR_EQUALS(login_response.response.error_message, "The access token has expired. Please log in again.");
ASSERT_PTR_NULL(login_response.username);
ASSERT_PTR_NULL(login_response.api_token);
ASSERT_NUM_EQUALS(login_response.score, 0);
ASSERT_NUM_EQUALS(login_response.score_softcore, 0);
ASSERT_NUM_EQUALS(login_response.num_unread_messages, 0);
ASSERT_PTR_NULL(login_response.display_name);

rc_api_destroy_login_response(&login_response);
}

static void test_process_login_response_generic_failure()
{
rc_api_login_response_t login_response;
Expand Down Expand Up @@ -556,8 +676,10 @@ void test_rapi_user(void) {

TEST(test_process_login_response_success);
TEST(test_process_login_response_unique_display_name);
TEST(test_process_login_response_error);
TEST(test_process_login_response_invalid_credentials);
TEST(test_process_login_response_access_denied);
TEST(test_process_login_response_generic_failure);
TEST(test_process_login_response_expired_token);
TEST(test_process_login_response_empty);
TEST(test_process_login_response_text);
TEST(test_process_login_response_html);
Expand Down
28 changes: 14 additions & 14 deletions test/test_rc_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ static void test_login_with_password(void)

g_client = mock_client_not_logged_in();
reset_mock_api_handlers();
mock_api_response("r=login&u=User&p=Pa%24%24word",
mock_api_response("r=login2&u=User&p=Pa%24%24word",
"{\"Success\":true,\"User\":\"User\",\"Token\":\"ApiToken\",\"Score\":12345,\"SoftcoreScore\":123,\"Messages\":2,\"Permissions\":1,\"AccountType\":\"Registered\"}");

rc_client_begin_login_with_password(g_client, "User", "Pa$$word", rc_client_callback_expect_success, g_callback_userdata);
Expand All @@ -645,7 +645,7 @@ static void test_login_with_token(void)

g_client = mock_client_not_logged_in();
reset_mock_api_handlers();
mock_api_response("r=login&u=User&t=ApiToken",
mock_api_response("r=login2&u=User&t=ApiToken",
"{\"Success\":true,\"User\":\"User\",\"DisplayName\":\"Display\",\"Token\":\"ApiToken\",\"Score\":12345,\"Messages\":2}");

rc_client_begin_login_with_token(g_client, "User", "ApiToken", rc_client_callback_expect_success, g_callback_userdata);
Expand Down Expand Up @@ -714,7 +714,7 @@ static void test_login_with_incorrect_password(void)
{
g_client = mock_client_not_logged_in();
reset_mock_api_handlers();
mock_api_error("r=login&u=User&p=Pa%24%24word",
mock_api_error("r=login2&u=User&p=Pa%24%24word",
"{\"Success\":false,\"Error\":\"Invalid User/Password combination. Please try again\","
"\"Status\":401,\"Code\":\"invalid_credentials\"}", 401);

Expand All @@ -737,7 +737,7 @@ static void test_login_with_incorrect_token(void)
{
g_client = mock_client_not_logged_in();
reset_mock_api_handlers();
mock_api_error("r=login&u=User&t=TOKEN",
mock_api_error("r=login2&u=User&t=TOKEN",
"{\"Success\":false,\"Error\":\"Invalid User/Token combination.\","
"\"Status\":401,\"Code\":\"invalid_credentials\"}", 401);

Expand All @@ -760,7 +760,7 @@ static void test_login_with_expired_token(void)
{
g_client = mock_client_not_logged_in();
reset_mock_api_handlers();
mock_api_error("r=login&u=User&t=EXPIRED",
mock_api_error("r=login2&u=User&t=EXPIRED",
"{\"Success\":false,\"Error\":\"The access token has expired. Please log in again.\","
"\"Status\":401,\"Code\":\"expired_token\"}", 403);

Expand All @@ -783,7 +783,7 @@ static void test_login_with_banned_account(void)
{
g_client = mock_client_not_logged_in();
reset_mock_api_handlers();
mock_api_error("r=login&u=User&p=Pa%24%24word",
mock_api_error("r=login2&u=User&p=Pa%24%24word",
"{\"Success\":false,\"Error\":\"Access denied.\","
"\"Status\":403,\"Code\":\"access_denied\"}", 403);

Expand All @@ -806,7 +806,7 @@ static void test_login_incomplete_response(void)
{
g_client = mock_client_not_logged_in();
reset_mock_api_handlers();
mock_api_response("r=login&u=User&p=Pa%24%24word", "{\"Success\":true,\"User\":\"Username\"}");
mock_api_response("r=login2&u=User&p=Pa%24%24word", "{\"Success\":true,\"User\":\"Username\"}");

rc_client_begin_login_with_password(g_client, "User", "Pa$$word", rc_client_callback_expect_missing_token, g_callback_userdata);

Expand All @@ -827,7 +827,7 @@ static void test_login_with_password_async(void)
user = rc_client_get_user_info(g_client);
ASSERT_PTR_NULL(user);

async_api_response("r=login&u=User&p=Pa%24%24word",
async_api_response("r=login2&u=User&p=Pa%24%24word",
"{\"Success\":true,\"User\":\"User\",\"Token\":\"ApiToken\",\"Score\":12345,\"SoftcoreScore\":123,\"Messages\":2,\"Permissions\":1,\"AccountType\":\"Registered\"}");

user = rc_client_get_user_info(g_client);
Expand Down Expand Up @@ -857,7 +857,7 @@ static void test_login_with_password_async_aborted(void)

rc_client_abort_async(g_client, handle);

async_api_response("r=login&u=User&p=Pa%24%24word",
async_api_response("r=login2&u=User&p=Pa%24%24word",
"{\"Success\":true,\"User\":\"User\",\"Token\":\"ApiToken\",\"Score\":12345,\"SoftcoreScore\":123,\"Messages\":2,\"Permissions\":1,\"AccountType\":\"Registered\"}");

user = rc_client_get_user_info(g_client);
Expand Down Expand Up @@ -934,7 +934,7 @@ static void test_logout_during_login(void)
rc_client_begin_login_with_password(g_client, "User", "Pa$$word", rc_client_callback_expect_login_aborted, g_callback_userdata);
rc_client_logout(g_client);

async_api_response("r=login&u=User&p=Pa%24%24word",
async_api_response("r=login2&u=User&p=Pa%24%24word",
"{\"Success\":true,\"User\":\"User\",\"Token\":\"ApiToken\",\"Score\":12345,\"SoftcoreScore\":123,\"Messages\":2,\"Permissions\":1,\"AccountType\":\"Registered\"}");

ASSERT_PTR_NULL(rc_client_get_user_info(g_client));
Expand Down Expand Up @@ -1283,7 +1283,7 @@ static void test_load_game_async_login(void)
assert_api_not_called("r=patch&u=Username&t=ApiToken&g=1234");

/* login completion will trigger process to continue */
async_api_response("r=login&u=Username&p=Pa%24%24word",
async_api_response("r=login2&u=Username&p=Pa%24%24word",
"{\"Success\":true,\"User\":\"Username\",\"Token\":\"ApiToken\",\"Score\":12345,\"SoftcoreScore\":123,\"Messages\":2,\"Permissions\":1,\"AccountType\":\"Registered\"}");
assert_api_pending("r=patch&u=Username&t=ApiToken&g=1234");

Expand Down Expand Up @@ -1322,7 +1322,7 @@ static void test_load_game_async_login_with_incorrect_password(void)
assert_api_not_called("r=patch&u=Username&t=ApiToken&g=1234");

/* login failure will trigger process to continue */
async_api_error("r=login&u=Username&p=Pa%24%24word",
async_api_error("r=login2&u=Username&p=Pa%24%24word",
"{\"Success\":false,\"Error\":\"Invalid User/Password combination. Please try again\","
"\"Status\":401,\"Code\":\"invalid_credentials\"}", 401);
assert_api_not_called("r=patch&u=Username&t=ApiToken&g=1234");
Expand All @@ -1349,7 +1349,7 @@ static void test_load_game_async_login_logout(void)

/* logout will cancel login and allow game load to proceed with failure */
rc_client_logout(g_client);
async_api_response("r=login&u=Username&p=Pa%24%24word",
async_api_response("r=login2&u=Username&p=Pa%24%24word",
"{\"Success\":true,\"User\":\"Username\",\"Token\":\"ApiToken\",\"Score\":12345,\"SoftcoreScore\":123,\"Messages\":2,\"Permissions\":1,\"AccountType\":\"Registered\"}");
assert_api_not_called("r=patch&u=Username&t=ApiToken&g=1234");

Expand Down Expand Up @@ -1377,7 +1377,7 @@ static void test_load_game_async_login_aborted(void)

/* login abort will trigger game load process to continue */
rc_client_abort_async(g_client, handle);
async_api_response("r=login&u=Username&p=Pa%24%24word",
async_api_response("r=login2&u=Username&p=Pa%24%24word",
"{\"Success\":true,\"User\":\"Username\",\"Token\":\"ApiToken\",\"Score\":12345,\"SoftcoreScore\":123,\"Messages\":2,\"Permissions\":1,\"AccountType\":\"Registered\"}");
assert_api_not_called("r=patch&u=Username&t=ApiToken&g=1234");

Expand Down

0 comments on commit e3c3621

Please sign in to comment.