about summary refs log tree commit diff stats
path: root/tests/test_cmd_connect.c
blob: d47666efe6bedce4c0f95bb17bf6c00b80ffb7e4 (plain) (blame)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

#include "xmpp/xmpp.h"
#include "xmpp/mock_xmpp.h"

#include "ui/ui.h"
#include "ui/mock_ui.h"

#include "command/commands.h"

#include "config/accounts.h"
#include "config/mock_accounts.h"

static void test_with_connection_status(jabber_conn_status_t status)
{
    mock_cons_show();
    CommandHelp *help = malloc(sizeof(CommandHelp));

    mock_connection_status(status);

    expect_cons_show("You are either connected already, or a login is in process.");

    gboolean result = cmd_connect(NULL, *help);
    assert_true(result);

    free(help);
}

void cmd_connect_shows_message_when_disconnecting(void **state)
{
    test_with_connection_status(JABBER_DISCONNECTING);
}

void cmd_connect_shows_message_when_connecting(void **state)
{
    test_with_connection_status(JABBER_CONNECTING);
}

void cmd_connect_shows_message_when_connected(void **state)
{
    test_with_connection_status(JABBER_CONNECTED);
}

void cmd_connect_shows_message_when_undefined(void **state)
{
    test_with_connection_status(JABBER_UNDEFINED);
}

void cmd_connect_when_no_account(void **state)
{
    mock_cons_show();
    mock_accounts_get_account();
    mock_ui_ask_password();
    mock_jabber_connect_with_details();
    CommandHelp *help = malloc(sizeof(CommandHelp));
    gchar *args[] = { "user@server.org", NULL };

    mock_connection_status(JABBER_DISCONNECTED);

    accounts_get_account_expect_and_return("user@server.org", NULL);

    mock_ui_ask_password_returns("password");

    expect_cons_show("Connecting as user@server.org");

    jabber_connect_with_username_password_expect_and_return("user@server.org", "password", JABBER_CONNECTING);

    gboolean result = cmd_connect(args, *help);
    assert_true(result);

    free(help);
}

void cmd_connect_with_altdomain_when_provided(void **state)
{
    stub_ui_ask_password();
    stub_cons_show();
    mock_accounts_get_account();
    mock_jabber_connect_with_details();
    CommandHelp *help = malloc(sizeof(CommandHelp));
    gchar *args[] = { "user@server.org", "altdomain" };

    mock_connection_status(JABBER_DISCONNECTED);

    accounts_get_account_return(NULL);

    jabber_connect_with_altdomain_expect_and_return("altdomain", JABBER_CONNECTING);

    gboolean result = cmd_connect(args, *help);
    assert_true(result);

    free(help);
}

void cmd_connect_fail_message(void **state)
{
    stub_cons_show();
    mock_cons_show_error();
    stub_ui_ask_password();
    mock_accounts_get_account();
    mock_jabber_connect_with_details();
    CommandHelp *help = malloc(sizeof(CommandHelp));
    gchar *args[] = { "user@server.org", NULL };

    mock_connection_status(JABBER_DISCONNECTED);

    accounts_get_account_return(NULL);

    jabber_connect_with_details_return(JABBER_DISCONNECTED);

    expect_cons_show_error("Connection attempt for user@server.org failed.");

    gboolean result = cmd_connect(args, *help);
    assert_true(result);

    free(help);
}

void cmd_connect_lowercases_argument(void **state)
{
    stub_cons_show();
    stub_ui_ask_password();
    mock_accounts_get_account();
    mock_jabber_connect_with_details();
    CommandHelp *help = malloc(sizeof(CommandHelp));
    gchar *args[] = { "USER@server.ORG", NULL };

    mock_connection_status(JABBER_DISCONNECTED);

    accounts_get_account_expect_and_return("user@server.org", NULL);

    jabber_connect_with_details_return(JABBER_CONNECTING);

    gboolean result = cmd_connect(args, *help);
    assert_true(result);

    free(help);
}

void cmd_connect_asks_password_when_not_in_account(void **state)
{
    stub_cons_show();
    stub_ui_ask_password();
    mock_accounts_get_account();
    mock_accounts_create_full_jid();
    mock_jabber_connect_with_account();
    stub_accounts_free_account();
    CommandHelp *help = malloc(sizeof(CommandHelp));
    gchar *args[] = { "jabber_org", NULL };
    ProfAccount *account = malloc(sizeof(ProfAccount));
    account->password = NULL;

    mock_connection_status(JABBER_DISCONNECTED);

    accounts_get_account_return(account);

    accounts_create_full_jid_return("user@jabber.org");

    jabber_connect_with_account_return(JABBER_CONNECTING);

    gboolean result = cmd_connect(args, *help);
    assert_true(result);

    free(help);
    free(account);
}

void cmd_connect_shows_message_when_connecting_with_account(void **state)
{
    mock_cons_show();
    mock_accounts_get_account();
    mock_accounts_create_full_jid();
    mock_jabber_connect_with_account();
    stub_accounts_free_account();
    CommandHelp *help = malloc(sizeof(CommandHelp));
    gchar *args[] = { "jabber_org", NULL };
    ProfAccount *account = malloc(sizeof(ProfAccount));
    account->password = "password";
    account->name = "jabber_org";

    mock_connection_status(JABBER_DISCONNECTED);

    accounts_get_account_return(account);

    accounts_create_full_jid_return("user@jabber.org/laptop");

    expect_cons_show("Connecting with account jabber_org as user@jabber.org/laptop");

    jabber_connect_with_account_return(JABBER_CONNECTING);

    gboolean result = cmd_connect(args, *help);
    assert_true(result);

    free(help);
    free(account);
}

void cmd_connect_connects_with_account(void **state)
{
    stub_cons_show();
    mock_accounts_get_account();
    mock_accounts_create_full_jid();
    mock_jabber_connect_with_account();
    stub_accounts_free_account();
    CommandHelp *help = malloc(sizeof(CommandHelp));
    gchar *args[] = { "jabber_org", NULL };
    ProfAccount *account = malloc(sizeof(ProfAccount));
    account->password = "password";
    account->name = "jabber_org";

    mock_connection_status(JABBER_DISCONNECTED);

    accounts_get_account_return(account);

    accounts_create_full_jid_return("user@jabber.org/laptop");

    jabber_connect_with_account_expect_and_return(account, JABBER_CONNECTING);

    gboolean result = cmd_connect(args, *help);
    assert_true(result);

    free(help);
    free(account);
}

void cmd_connect_frees_account_after_connecting(void **state)
{
    stub_cons_show();
    mock_accounts_get_account();
    mock_accounts_create_full_jid();
    mock_jabber_connect_with_account();
    mock_accounts_free_account();
    CommandHelp *help = malloc(sizeof(CommandHelp));
    gchar *args[] = { "jabber_org", NULL };
    ProfAccount *account = malloc(sizeof(ProfAccount));

    mock_connection_status(JABBER_DISCONNECTED);

    accounts_get_account_return(account);

    accounts_create_full_jid_return("user@jabber.org/laptop");

    jabber_connect_with_account_return(JABBER_CONNECTING);

    accounts_free_account_expect(account);

    gboolean result = cmd_connect(args, *help);
    assert_true(result);

    free(help);
    free(account);
}