File: glewlwyd_user_session_user.c

package info (click to toggle)
glewlwyd 1.4.9-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,096 kB
  • sloc: ansic: 14,286; sql: 1,499; sh: 246; makefile: 150
file content (147 lines) | stat: -rw-r--r-- 4,500 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Public domain, no copyright. Use at your own risk. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>

#include <check.h>
#include <ulfius.h>
#include <orcania.h>
#include <yder.h>

#include "unit-tests.h"

#define SERVER_URI "http://localhost:4593/api"
#define USER_LOGIN "user1"
#define USER_PASSWORD "MyUser1Password!"

struct _u_request user_req;

START_TEST(test_glwd_user_session_list_all_user)
{
  char * url = msprintf("%s/profile/session", SERVER_URI);
  
  int res = run_simple_test(&user_req, "GET", url, NULL, NULL, NULL, NULL, 200, NULL, NULL, NULL);
  free(url);
  ck_assert_int_eq(res, 1);
}
END_TEST

START_TEST(test_glwd_user_session_list_enabled_user)
{
  char * url = msprintf("%s/profile/session?valid=true", SERVER_URI);
  
  int res = run_simple_test(&user_req, "GET", url, NULL, NULL, NULL, NULL, 200, NULL, NULL, NULL);
  free(url);
  ck_assert_int_eq(res, 1);
}
END_TEST

START_TEST(test_glwd_user_session_revoke_not_found_user)
{
  char * url = msprintf("%s/profile/session", SERVER_URI);
  json_t * json_body = json_pack("{ss}", "session_hash", "not_found");
  
  ulfius_set_json_body_request(&user_req, json_body);
  int res = run_simple_test(&user_req, "DELETE", url, NULL, NULL, NULL, NULL, 404, NULL, NULL, NULL);
  free(url);
  json_decref(json_body);
  ck_assert_int_eq(res, 1);
}
END_TEST

START_TEST(test_glwd_user_session_revoke_ok_user)
{
  struct _u_response list_resp, del_resp;
  int res;
  struct _u_map body;
  json_t * json_resp_body;
  
  u_map_init(&body);
  ulfius_init_response(&list_resp);
  ulfius_init_response(&del_resp);
  user_req.http_url = msprintf("%s/profile/session?valid=true", SERVER_URI);
  res = ulfius_send_http_request(&user_req, &list_resp);
  if (res == U_OK) {
    json_resp_body = ulfius_get_json_body_response(&list_resp, NULL);
    json_t * json_body = json_pack("{ss}", "session_hash", json_string_value(json_object_get(json_array_get(json_resp_body, 0), "session_hash")));
    u_map_put(user_req.map_header, "Content-Type", "application/x-www-form-urlencoded");
    ulfius_set_json_body_request(&user_req, json_body);
    json_decref(json_body);
    json_decref(json_resp_body);
  }
  user_req.http_url = msprintf("%s/profile/session/", SERVER_URI, USER_LOGIN);
  user_req.http_verb = strdup("DELETE");
  ulfius_send_http_request(&user_req, &del_resp);
  
  ck_assert_int_eq(del_resp.status, 200);
  
  u_map_clean(&body);
  ulfius_clean_response(&list_resp);
  ulfius_clean_response(&del_resp);
}
END_TEST

static Suite *glewlwyd_suite(void)
{
  Suite *s;
  TCase *tc_core;

  s = suite_create("Glewlwyd user session management user");
  tc_core = tcase_create("test_glwd_user_session");
  tcase_add_test(tc_core, test_glwd_user_session_list_all_user);
  tcase_add_test(tc_core, test_glwd_user_session_list_enabled_user);
  tcase_add_test(tc_core, test_glwd_user_session_revoke_not_found_user);
  tcase_add_test(tc_core, test_glwd_user_session_revoke_ok_user);
  tcase_set_timeout(tc_core, 30);
  suite_add_tcase(s, tc_core);

  return s;
}

int main(int argc, char *argv[])
{
  int number_failed;
  Suite *s;
  SRunner *sr;
  struct _u_request auth_req;
  struct _u_response auth_resp;
  int i;
  char * cookie;
  
  y_init_logs("Glewlwyd test", Y_LOG_MODE_CONSOLE, Y_LOG_LEVEL_DEBUG, NULL, "Starting Glewlwyd test");
  
  // Getting a valid session id for authenticated http requests
  ulfius_init_request(&user_req);
  ulfius_init_request(&auth_req);
  ulfius_init_response(&auth_resp);
  auth_req.http_verb = strdup("POST");
  auth_req.http_url = msprintf("%s/auth/user", SERVER_URI);
  u_map_put(auth_req.map_post_body, "username", USER_LOGIN);
  u_map_put(auth_req.map_post_body, "password", USER_PASSWORD);
  ulfius_send_http_request(&auth_req, &auth_resp);
  if (auth_resp.status == 200) {
    y_log_message(Y_LOG_LEVEL_INFO, "User %s authenticated", USER_LOGIN);
  } else {
    y_log_message(Y_LOG_LEVEL_ERROR, "User %s error", USER_LOGIN);
  }
  for (i=0; i<auth_resp.nb_cookies; i++) {
    cookie = msprintf("%s=%s", auth_resp.map_cookie[i].key, auth_resp.map_cookie[i].value);
    u_map_put(user_req.map_header, "Cookie", cookie);
  }
  ulfius_clean_request(&auth_req);
  ulfius_clean_response(&auth_resp);

  s = glewlwyd_suite();
  sr = srunner_create(s);

  srunner_run_all(sr, CK_VERBOSE);
  number_failed = srunner_ntests_failed(sr);
  srunner_free(sr);
  
  ulfius_clean_request(&user_req);
  
  return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}