/
web-user.t
111 lines (96 loc) · 3.86 KB
/
web-user.t
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
#!perl
use lib '.';
use t::Helper;
$ENV{CONVOS_BACKEND} = 'Convos::Core::Backend';
my $t = t::Helper->t;
my $registered;
subtest 'Wait for backend' => sub {
$t->app->core->settings->default_connection(Mojo::URL->new('irc://localhost:6123/%23convos'))
->open_to_public(true);
is $t->app->core->ready, 1, 'ready';
};
subtest 'Not logged in' => sub {
$t->get_ok('/api/user')->status_is(401)->json_is('/errors/0/message', 'Need to log in first.');
$t->delete_ok('/api/user/superman@example.com')->status_is(401)
->json_is('/errors/0/message', 'Need to log in first.');
$t->post_ok('/api/user/superman@example.com', json => {})->status_is(401)
->json_is('/errors/0/message', 'Need to log in first.')->json_is('/errors/0/path', '/');
};
subtest 'Register' => sub {
$t->post_ok('/api/user/register',
json => {email => 'superman@example.com', password => ' longenough '})->status_is(200)
->json_is('/email', 'superman@example.com')->json_like('/registered', qr/^[\d-]+T[\d:]+Z$/);
$registered = $t->tx->res->json->{registered};
};
subtest 'Update' => sub {
$t->post_ok('/api/user/superman@example.com', json => {})->status_is(200);
$t->get_ok('/api/user')->status_is(200);
is_deeply(
$t->tx->res->json,
{
email => 'superman@example.com',
forced_connection => false,
default_connection => 'irc://localhost:6123/%23convos',
highlight_keywords => [],
registered => $registered,
remote_address => '127.0.0.1',
roles => ['admin'],
uid => 1,
unread => 0,
video_service => 'https://meet.jit.si/',
},
'user object'
);
$t->post_ok('/api/user/superman@example.com',
json => {highlight_keywords => [' ', '-', '.', ' ,', ' ', 'foo ']})->status_is(200);
$t->get_ok('/api/user')->status_is(200)->json_is('/email', 'superman@example.com')
->json_is('/highlight_keywords', ['foo']);
};
subtest 'Unread' => sub {
my $user = $t->app->core->get_user('superman@example.com');
$user->connection({connection_id => 'irc-localhost'})->conversation({name => '#convos'})
->unread(42);
$t->get_ok('/api/user?connections=true&conversations=true')->status_is(200);
cmp_deeply(
$t->tx->res->json,
superhashof({
email => 'superman@example.com',
conversations => [{
connection_id => 'irc-localhost',
conversation_id => '#convos',
frozen => 'Not connected.',
name => '#convos',
notifications => 0,
topic => '',
unread => 42,
}],
connections => [{
connection_id => 'irc-localhost',
me => {authenticated => false, capabilities => {}, nick => 'superman'},
name => 'localhost',
on_connect_commands => [],
service_accounts => [qw(chanserv nickserv)],
state => re(qr{connecting|disconnected|queued}),
url => 'irc://localhost:6123/%23convos?nick=superman&tls=1',
wanted_state => 'connected',
}],
}),
'user.json'
);
$t->post_ok('/api/notifications/read')->status_is(200);
};
subtest 'Delete' => sub {
$t->delete_ok('/api/user/superman@example.com')->status_is(400)
->json_is('/errors/0/message', 'You are the only user left.');
$t->t::Helper::with_csrf('/logout')->status_is(302);
$t->get_ok('/api/user')->status_is(401);
};
subtest 'Backend not ready' => sub {
$t->app->core->ready(0);
$t->get_ok('/api/user')->status_is(503)->json_is('/errors/0/message', 'Backend is starting.');
$t->get_ok('/chat')->status_is(503)->element_exists('a.btn[href="/"]')
->text_is('a.btn[href="/"]', 'Try again')
->text_is('title', 'Backend is starting (503) - Convos')
->text_is('h1', 'Backend is starting (503)');
};
done_testing;