aboutsummaryrefslogtreecommitdiff
path: root/server/test/jchat_http_SUITE.erl
blob: 5c8804f01f3c8a28be1559eaa08703ee4cdcb63b (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
-module(jchat_http_SUITE).
-compile(export_all).

-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").

%%====================================================================
%% CT Callbacks
%%====================================================================

suite() ->
    [{timetrap, {seconds, 30}}].

init_per_suite(Config) ->
    % Start the application
    application:ensure_all_started(jchat),
    % Wait for server to start
    timer:sleep(1000),
    [{server_url, "http://localhost:8081"} | Config].

end_per_suite(_Config) ->
    application:stop(jchat),
    ok.

init_per_testcase(_TestCase, Config) ->
    % Clean up test data
    mnesia:clear_table(user),
    Config.

end_per_testcase(_TestCase, _Config) ->
    ok.

all() ->
    [test_auth_register_endpoint,
     test_auth_login_endpoint,
     test_auth_me_endpoint,
     test_jmap_api_with_auth,
     test_jmap_api_without_auth,
     test_cors_headers].

%%====================================================================
%% Test Cases
%%====================================================================

test_auth_register_endpoint(Config) ->
    ServerUrl = ?config(server_url, Config),
    
    % Prepare registration data
    Email = "test@example.com",
    Password = "testpass123",
    DisplayName = "Test User",
    
    ReqBody = jsx:encode(#{
        <<"email">> => list_to_binary(Email),
        <<"password">> => list_to_binary(Password),
        <<"displayName">> => list_to_binary(DisplayName)
    }),
    
    % Make registration request
    Url = ServerUrl ++ "/auth/register",
    Headers = [{"content-type", "application/json"}],
    
    {ok, {{_Version, 201, _ReasonPhrase}, _Headers, ResponseBody}} = 
        httpc:request(post, {Url, Headers, "application/json", ReqBody}, [], []),
    
    % Parse response
    ResponseMap = jsx:decode(list_to_binary(ResponseBody)),
    ?assert(maps:is_key(<<"token">>, ResponseMap)),
    ?assert(maps:is_key(<<"user">>, ResponseMap)),
    
    User = maps:get(<<"user">>, ResponseMap),
    ?assertEqual(list_to_binary(Email), maps:get(<<"email">>, User)),
    ?assertEqual(list_to_binary(DisplayName), maps:get(<<"displayName">>, User)).

test_auth_login_endpoint(Config) ->
    ServerUrl = ?config(server_url, Config),
    
    % First register a user
    Email = "login.test@example.com",
    Password = "logintest123",
    DisplayName = "Login Test User",
    
    {ok, _User} = jchat_auth:register_user(
        list_to_binary(Email), 
        list_to_binary(Password), 
        list_to_binary(DisplayName)
    ),
    
    % Now test login
    ReqBody = jsx:encode(#{
        <<"email">> => list_to_binary(Email),
        <<"password">> => list_to_binary(Password)
    }),
    
    Url = ServerUrl ++ "/auth/login",
    Headers = [{"content-type", "application/json"}],
    
    {ok, {{_Version, 200, _ReasonPhrase}, _Headers, ResponseBody}} = 
        httpc:request(post, {Url, Headers, "application/json", ReqBody}, [], []),
    
    % Parse response
    ResponseMap = jsx:decode(list_to_binary(ResponseBody)),
    ?assert(maps:is_key(<<"token">>, ResponseMap)),
    ?assert(maps:is_key(<<"user">>, ResponseMap)).

test_auth_me_endpoint(Config) ->
    ServerUrl = ?config(server_url, Config),
    
    % Register and login to get token
    Email = "me.test@example.com",
    Password = "metest123",
    DisplayName = "Me Test User",
    
    {ok, _User} = jchat_auth:register_user(
        list_to_binary(Email), 
        list_to_binary(Password), 
        list_to_binary(DisplayName)
    ),
    
    {ok, {_AuthUser, Token}} = jchat_auth:authenticate_user(
        list_to_binary(Email), 
        list_to_binary(Password)
    ),
    
    % Test /auth/me endpoint
    Url = ServerUrl ++ "/auth/me",
    Headers = [{"authorization", "Bearer " ++ binary_to_list(Token)}],
    
    {ok, {{_Version, 200, _ReasonPhrase}, _Headers, ResponseBody}} = 
        httpc:request(get, {Url, Headers}, [], []),
    
    % Parse response
    ResponseMap = jsx:decode(list_to_binary(ResponseBody)),
    ?assert(maps:is_key(<<"user">>, ResponseMap)),
    
    User = maps:get(<<"user">>, ResponseMap),
    ?assertEqual(list_to_binary(Email), maps:get(<<"email">>, User)).

test_jmap_api_with_auth(Config) ->
    ServerUrl = ?config(server_url, Config),
    
    % Register and login to get token
    Email = "jmap.test@example.com",
    Password = "jmaptest123",
    DisplayName = "JMAP Test User",
    
    {ok, _User} = jchat_auth:register_user(
        list_to_binary(Email), 
        list_to_binary(Password), 
        list_to_binary(DisplayName)
    ),
    
    {ok, {_AuthUser, Token}} = jchat_auth:authenticate_user(
        list_to_binary(Email), 
        list_to_binary(Password)
    ),
    
    % Test JMAP API call
    ReqBody = jsx:encode(#{
        <<"using">> => [<<"urn:ietf:params:jmap:core">>, <<"https://jmap.io/jchat/">>],
        <<"methodCalls">> => [
            [<<"Conversation/query">>, #{
                <<"accountId">> => <<"default">>,
                <<"filter">> => #{},
                <<"sort">> => [#{<<"property">> => <<"lastMessageAt">>, <<"isAscending">> => false}]
            }, <<"c1">>]
        ]
    }),
    
    Url = ServerUrl ++ "/jmap/api",
    Headers = [
        {"content-type", "application/json"},
        {"authorization", "Bearer " ++ binary_to_list(Token)}
    ],
    
    {ok, {{_Version, 200, _ReasonPhrase}, _Headers, ResponseBody}} = 
        httpc:request(post, {Url, Headers, "application/json", ReqBody}, [], []),
    
    % Parse response
    ResponseMap = jsx:decode(list_to_binary(ResponseBody)),
    ?assert(maps:is_key(<<"methodResponses">>, ResponseMap)).

test_jmap_api_without_auth(Config) ->
    ServerUrl = ?config(server_url, Config),
    
    % Test JMAP API call without authentication
    ReqBody = jsx:encode(#{
        <<"using">> => [<<"urn:ietf:params:jmap:core">>, <<"https://jmap.io/jchat/">>],
        <<"methodCalls">> => [
            [<<"Conversation/query">>, #{
                <<"accountId">> => <<"default">>,
                <<"filter">> => #{},
                <<"sort">> => [#{<<"property">> => <<"lastMessageAt">>, <<"isAscending">> => false}]
            }, <<"c1">>]
        ]
    }),
    
    Url = ServerUrl ++ "/jmap/api",
    Headers = [{"content-type", "application/json"}],
    
    {ok, {{_Version, 401, _ReasonPhrase}, _Headers, _ResponseBody}} = 
        httpc:request(post, {Url, Headers, "application/json", ReqBody}, [], []).

test_cors_headers(Config) ->
    ServerUrl = ?config(server_url, Config),
    
    % Test CORS preflight
    Url = ServerUrl ++ "/auth/register",
    Headers = [
        {"origin", "http://localhost:3000"},
        {"access-control-request-method", "POST"},
        {"access-control-request-headers", "content-type,authorization"}
    ],
    
    {ok, {{_Version, StatusCode, _ReasonPhrase}, ResponseHeaders, _ResponseBody}} = 
        httpc:request(options, {Url, Headers}, [], []),
    
    % Should return 200 or 204 for OPTIONS
    ?assert(StatusCode =:= 200 orelse StatusCode =:= 204),
    
    % Check for CORS headers
    HeadersMap = maps:from_list(ResponseHeaders),
    ?assert(maps:is_key("access-control-allow-origin", HeadersMap) orelse
            maps:is_key("Access-Control-Allow-Origin", HeadersMap)).