diff --git a/src/http_server_handle_req_get_auth.c b/src/http_server_handle_req_get_auth.c index e42a6fa..fb563ae 100644 --- a/src/http_server_handle_req_get_auth.c +++ b/src/http_server_handle_req_get_auth.c @@ -191,7 +191,17 @@ http_server_handle_req_get_auth_ruuvi( { if (HTTP_SERVER_AUTH_TYPE_ALLOW == auth_type) { - return http_server_handle_req_get_auth_allow(p_param->p_hostinfo, p_param->flag_access_from_lan); + if (flag_check) + { + return http_server_handle_req_get_auth_allow(p_param->p_hostinfo, p_param->flag_access_from_lan); + } + else + { + return http_server_resp_200_auth_allow_with_new_session_id( + p_param->p_remote_ip, + p_param->p_hostinfo, + p_extra_header_fields); + } } if (flag_check) { diff --git a/tests/test_http_server_handle_req_get_auth/test_http_server_handle_req_get_auth.cpp b/tests/test_http_server_handle_req_get_auth/test_http_server_handle_req_get_auth.cpp index 185ff79..409cae6 100644 --- a/tests/test_http_server_handle_req_get_auth/test_http_server_handle_req_get_auth.cpp +++ b/tests/test_http_server_handle_req_get_auth/test_http_server_handle_req_get_auth.cpp @@ -28,6 +28,8 @@ class TestHttpServerHandleReqGetAuth : public ::testing::Test SetUp() override { this->m_idx_random_value = 0; + std::fill(arr_of_random_values.begin(), arr_of_random_values.end(), 0); + set_random_values(this->arr_of_random_values.data(), this->arr_of_random_values.size()); http_server_auth_clear_authorized_sessions(); } @@ -39,9 +41,10 @@ class TestHttpServerHandleReqGetAuth : public ::testing::Test } public: - const uint32_t* m_p_random_values; - size_t m_num_random_values; - size_t m_idx_random_value; + const uint32_t* m_p_random_values; + size_t m_num_random_values; + size_t m_idx_random_value; + std::array arr_of_random_values; TestHttpServerHandleReqGetAuth(); @@ -90,7 +93,7 @@ esp_random(void) /*** Unit-Tests *******************************************************************************************************/ -TEST_F(TestHttpServerHandleReqGetAuth, test_auth_allow) // NOLINT +TEST_F(TestHttpServerHandleReqGetAuth, test_req_get_auth_allow) // NOLINT { const http_server_auth_info_t auth_info = { HTTP_SERVER_AUTH_TYPE_ALLOW, @@ -128,6 +131,57 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_allow) // NOLINT ASSERT_EQ(HTTP_CONTENT_ENCODING_NONE, resp.content_encoding); ASSERT_EQ(exp_json_resp, string(reinterpret_cast(resp.select_location.memory.p_buf))); ASSERT_EQ(exp_json_resp.length(), resp.content_len); + ASSERT_EQ( + string("WWW-Authenticate: x-ruuvi-interactive realm=\"RuuviGatewayEEFF\" " + "challenge=\"66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925\" " + "session_cookie=\"RUUVISESSION\" session_id=\"AAAAAAAAAAAAAAAA\"\r\n" + "Set-Cookie: RUUVISESSION=AAAAAAAAAAAAAAAA\r\n"), + string(extra_header_fields.buf)); +} + +TEST_F(TestHttpServerHandleReqGetAuth, test_req_check_auth_allow) // NOLINT +{ + const http_server_auth_info_t auth_info = { + HTTP_SERVER_AUTH_TYPE_ALLOW, + "", + "", + }; + const wifiman_hostinfo_t hostinfo = { + .hostname = { "RuuviGatewayEEFF" }, + .fw_ver = { "1.13.0" }, + .nrf52_fw_ver = { "1.0.0" }, + }; + + const http_req_header_t http_header = { "" }; + const sta_ip_string_t remote_ip = { "192.168.1.10" }; + http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; + + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + + bool flag_access_by_bearer_token = false; + const http_server_resp_t resp = http_server_handle_req_check_auth( + ¶m, + &extra_header_fields, + &flag_access_by_bearer_token); + const string exp_json_resp + = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_allow", "lan": true})"; + ASSERT_FALSE(flag_access_by_bearer_token); + ASSERT_EQ(HTTP_RESP_CODE_200, resp.http_resp_code); + ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); + ASSERT_TRUE(resp.flag_no_cache); + ASSERT_TRUE(resp.flag_add_header_date); + ASSERT_EQ(HTTP_CONTENT_TYPE_APPLICATION_JSON, resp.content_type); + ASSERT_EQ(nullptr, resp.p_content_type_param); + ASSERT_EQ(HTTP_CONTENT_ENCODING_NONE, resp.content_encoding); + ASSERT_EQ(exp_json_resp, string(reinterpret_cast(resp.select_location.memory.p_buf))); + ASSERT_EQ(exp_json_resp.length(), resp.content_len); ASSERT_EQ(string(""), string(extra_header_fields.buf)); } diff --git a/tests/test_http_server_resp/test_http_server_resp.cpp b/tests/test_http_server_resp/test_http_server_resp.cpp index 118a679..061eb8d 100644 --- a/tests/test_http_server_resp/test_http_server_resp.cpp +++ b/tests/test_http_server_resp/test_http_server_resp.cpp @@ -7,6 +7,7 @@ #include "gtest/gtest.h" #include "http_server_resp.h" +#include "http_server_auth.h" #include using namespace std; @@ -376,4 +377,15 @@ TEST_F(TestHttpServerResp, test_http_server_resp_200_auth_allow_with_new_session "{\"gateway_name\": \"hostname\", \"fw_ver\": \"v1.15.0\", \"nrf52_fw_ver\": \"v1.0.0\", \"lan_auth_type\": " "\"lan_auth_allow\", \"lan\": true}", string(reinterpret_cast(resp.select_location.memory.p_buf))); + ASSERT_EQ( + "WWW-Authenticate: x-ruuvi-interactive realm=\"hostname\" " + "challenge=\"66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925\" " + "session_cookie=\"RUUVISESSION\" session_id=\"AAAAAAAAAAAAAAAA\"\r\n" + "Set-Cookie: RUUVISESSION=AAAAAAAAAAAAAAAA\r\n", + string(extra_header_fields.buf)); + + const http_server_auth_ruuvi_t* const p_auth = http_server_auth_ruuvi_get_info(); + const http_server_auth_ruuvi_authorized_session_t* const p_session = &p_auth->authorized_sessions[0]; + ASSERT_EQ("AAAAAAAAAAAAAAAA", string(p_session->session_id.buf)); + ASSERT_EQ(string(remote_ip.buf), string(p_session->remote_ip.buf)); }