summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJesper Jensen <jesper@jnsn.dev>2026-02-19 17:39:44 +0100
committerJesper Jensen <jesper@jnsn.dev>2026-02-19 17:39:44 +0100
commitb0e992e99bf0f6bcb3602f6f4fd4874cc510cfaa (patch)
tree21bb21bd17ee7b559ff487c7e9d2ac11d8e5eafa /test
parent8d3257fe558727a3d64902e27539b5a5c28ba49f (diff)
Add some simple login system
Diffstat (limited to 'test')
-rw-r--r--test/http.c94
1 files changed, 88 insertions, 6 deletions
diff --git a/test/http.c b/test/http.c
index 27385ab..fa8650d 100644
--- a/test/http.c
+++ b/test/http.c
@@ -54,12 +54,13 @@ int main(int argc, char **argv) {
struct Server server = {
.app = &app,
};
- web_begin(&server, 8080);
+ web_begin(&server, 8080, "admin", "changeme");
curl_global_init(CURL_GLOBAL_ALL);
CURLcode curlRes;
CURL *curl = curl_easy_init();
assert(curl != NULL);
+ // Default page requires auth, should redirect
{
curlRes = curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/");
assert(curlRes == CURLE_OK);
@@ -67,6 +68,9 @@ int main(int argc, char **argv) {
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_memory);
assert(curlRes == CURLE_OK);
+ curlRes = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0L);
+ assert(curlRes == CURLE_OK);
+
struct memory body = {0};
curlRes = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
assert(curlRes == CURLE_OK);
@@ -76,11 +80,7 @@ int main(int argc, char **argv) {
long code;
curlRes = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
- assert(code == 200);
-
- char *ct;
- curlRes = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
- assert(curlRes == CURLE_OK);
+ assert(code == 302); // Redirect to login
}
@@ -342,5 +342,87 @@ int main(int argc, char **argv) {
free(result);
}
+ // Test: Full login flow
+ {
+ // Use fresh curl with cookie jar
+ CURL *auth_curl = curl_easy_init();
+ assert(auth_curl != NULL);
+ curl_easy_setopt(auth_curl, CURLOPT_COOKIEFILE, ""); // Enable cookie engine
+ curl_easy_setopt(auth_curl, CURLOPT_FOLLOWLOCATION, 0L);
+ curl_easy_setopt(auth_curl, CURLOPT_WRITEFUNCTION, write_to_memory);
+ struct memory body = {0};
+ curl_easy_setopt(auth_curl, CURLOPT_WRITEDATA, &body);
+
+ // Step 1: Unauthenticated access redirects to /login
+ curl_easy_setopt(auth_curl, CURLOPT_URL, "http://localhost:8080/clients");
+ curl_easy_perform(auth_curl);
+ long code;
+ curl_easy_getinfo(auth_curl, CURLINFO_RESPONSE_CODE, &code);
+ assert(code == 302);
+
+ // Step 2: Wrong credentials show error (not redirect)
+ free(body.body); body = (struct memory){0};
+ curl_easy_setopt(auth_curl, CURLOPT_URL, "http://localhost:8080/login");
+ curl_easy_setopt(auth_curl, CURLOPT_POSTFIELDS, "username=admin&password=wrong");
+ curl_easy_perform(auth_curl);
+ curl_easy_getinfo(auth_curl, CURLINFO_RESPONSE_CODE, &code);
+ assert(code == 200);
+ assert(strstr(body.body, "Invalid") != NULL);
+
+ // Step 3: Correct credentials redirect and set cookie
+ free(body.body); body = (struct memory){0};
+ curl_easy_setopt(auth_curl, CURLOPT_POSTFIELDS, "username=admin&password=changeme");
+ curl_easy_perform(auth_curl);
+ curl_easy_getinfo(auth_curl, CURLINFO_RESPONSE_CODE, &code);
+ assert(code == 303);
+
+ // Step 4: Authenticated access works
+ free(body.body); body = (struct memory){0};
+ curl_easy_setopt(auth_curl, CURLOPT_HTTPGET, 1L);
+ curl_easy_setopt(auth_curl, CURLOPT_URL, "http://localhost:8080/clients");
+ curl_easy_perform(auth_curl);
+ curl_easy_getinfo(auth_curl, CURLINFO_RESPONSE_CODE, &code);
+ assert(code == 200);
+ assert(strstr(body.body, "Clients") != NULL);
+
+ // Step 5: Logout clears cookie and redirects
+ free(body.body); body = (struct memory){0};
+ curl_easy_setopt(auth_curl, CURLOPT_URL, "http://localhost:8080/logout");
+ curl_easy_setopt(auth_curl, CURLOPT_POST, 1L);
+ curl_easy_setopt(auth_curl, CURLOPT_POSTFIELDS, "");
+ curl_easy_perform(auth_curl);
+ curl_easy_getinfo(auth_curl, CURLINFO_RESPONSE_CODE, &code);
+ assert(code == 303); // Redirect to login
+
+ // Step 6: No longer authenticated after logout
+ free(body.body); body = (struct memory){0};
+ curl_easy_setopt(auth_curl, CURLOPT_HTTPGET, 1L);
+ curl_easy_setopt(auth_curl, CURLOPT_URL, "http://localhost:8080/clients");
+ curl_easy_perform(auth_curl);
+ curl_easy_getinfo(auth_curl, CURLINFO_RESPONSE_CODE, &code);
+ assert(code == 302); // Redirect to login (not authenticated)
+
+ free(body.body);
+ curl_easy_cleanup(auth_curl);
+ }
+
+ // Test: Invalid session cookie rejected
+ {
+ CURL *bad_curl = curl_easy_init();
+ assert(bad_curl != NULL);
+ curl_easy_setopt(bad_curl, CURLOPT_FOLLOWLOCATION, 0L);
+ curl_easy_setopt(bad_curl, CURLOPT_COOKIE, "session=invalidtoken");
+ curl_easy_setopt(bad_curl, CURLOPT_URL, "http://localhost:8080/clients");
+ curl_easy_setopt(bad_curl, CURLOPT_WRITEFUNCTION, write_to_memory);
+ struct memory body = {0};
+ curl_easy_setopt(bad_curl, CURLOPT_WRITEDATA, &body);
+ curl_easy_perform(bad_curl);
+ long code;
+ curl_easy_getinfo(bad_curl, CURLINFO_RESPONSE_CODE, &code);
+ assert(code == 302); // Redirect to login
+ free(body.body);
+ curl_easy_cleanup(bad_curl);
+ }
+
web_join(&server);
}