1 #include <stdarg.h>
2 #include <stddef.h>
3 #include <setjmp.h>
4 #include <cmocka.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <glib.h>
8 
9 #include "xmpp/xmpp.h"
10 
11 #include "ui/ui.h"
12 #include "ui/stub_ui.h"
13 
14 #include "config/accounts.h"
15 
16 #include "command/cmd_funcs.h"
17 
18 #define CMD_ACCOUNT "/account"
19 
20 void
cmd_account_shows_usage_when_not_connected_and_no_args(void ** state)21 cmd_account_shows_usage_when_not_connected_and_no_args(void** state)
22 {
23     gchar* args[] = { NULL };
24 
25     will_return(connection_get_status, JABBER_DISCONNECTED);
26 
27     expect_string(cons_bad_cmd_usage, cmd, CMD_ACCOUNT);
28 
29     gboolean result = cmd_account(NULL, CMD_ACCOUNT, args);
30     assert_true(result);
31 }
32 
33 void
cmd_account_shows_account_when_connected_and_no_args(void ** state)34 cmd_account_shows_account_when_connected_and_no_args(void** state)
35 {
36     ProfAccount* account = account_new("jabber_org", "me@jabber.org", NULL, NULL,
37                                        TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
38     gchar* args[] = { NULL };
39 
40     will_return(connection_get_status, JABBER_CONNECTED);
41     will_return(session_get_account_name, "account_name");
42     expect_any(accounts_get_account, name);
43     will_return(accounts_get_account, account);
44 
45     expect_memory(cons_show_account, account, account, sizeof(ProfAccount));
46 
47     gboolean result = cmd_account(NULL, CMD_ACCOUNT, args);
48     assert_true(result);
49 }
50 
51 void
cmd_account_list_shows_accounts(void ** state)52 cmd_account_list_shows_accounts(void** state)
53 {
54     gchar* args[] = { "list", NULL };
55 
56     gchar** accounts = malloc(sizeof(gchar*) * 4);
57     accounts[0] = strdup("account1");
58     accounts[1] = strdup("account2");
59     accounts[2] = strdup("account3");
60     accounts[3] = NULL;
61 
62     will_return(accounts_get_list, accounts);
63 
64     expect_memory(cons_show_account_list, accounts, accounts, sizeof(accounts));
65 
66     gboolean result = cmd_account_list(NULL, CMD_ACCOUNT, args);
67     assert_true(result);
68 }
69 
70 void
cmd_account_show_shows_usage_when_no_arg(void ** state)71 cmd_account_show_shows_usage_when_no_arg(void** state)
72 {
73     gchar* args[] = { "show", NULL };
74 
75     expect_string(cons_bad_cmd_usage, cmd, CMD_ACCOUNT);
76 
77     gboolean result = cmd_account_show(NULL, CMD_ACCOUNT, args);
78     assert_true(result);
79 }
80 
81 void
cmd_account_show_shows_message_when_account_does_not_exist(void ** state)82 cmd_account_show_shows_message_when_account_does_not_exist(void** state)
83 {
84     gchar* args[] = { "show", "account_name", NULL };
85 
86     expect_any(accounts_get_account, name);
87     will_return(accounts_get_account, NULL);
88 
89     expect_cons_show("No such account.");
90     expect_cons_show("");
91 
92     gboolean result = cmd_account_show(NULL, CMD_ACCOUNT, args);
93     assert_true(result);
94 }
95 
96 void
cmd_account_show_shows_account_when_exists(void ** state)97 cmd_account_show_shows_account_when_exists(void** state)
98 {
99     gchar* args[] = { "show", "account_name", NULL };
100     ProfAccount* account = account_new("jabber_org", "me@jabber.org", NULL, NULL,
101                                        TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
102 
103     expect_any(accounts_get_account, name);
104     will_return(accounts_get_account, account);
105 
106     expect_memory(cons_show_account, account, account, sizeof(account));
107 
108     gboolean result = cmd_account_show(NULL, CMD_ACCOUNT, args);
109     assert_true(result);
110 }
111 
112 void
cmd_account_add_shows_usage_when_no_arg(void ** state)113 cmd_account_add_shows_usage_when_no_arg(void** state)
114 {
115     gchar* args[] = { "add", NULL };
116 
117     expect_string(cons_bad_cmd_usage, cmd, CMD_ACCOUNT);
118 
119     gboolean result = cmd_account_add(NULL, CMD_ACCOUNT, args);
120     assert_true(result);
121 }
122 
123 void
cmd_account_add_adds_account(void ** state)124 cmd_account_add_adds_account(void** state)
125 {
126     gchar* args[] = { "add", "new_account", NULL };
127 
128     expect_string(accounts_add, jid, "new_account");
129     expect_value(accounts_add, altdomain, NULL);
130     expect_value(accounts_add, port, 0);
131     expect_cons_show("Account created.");
132     expect_cons_show("");
133 
134     gboolean result = cmd_account_add(NULL, CMD_ACCOUNT, args);
135     assert_true(result);
136 }
137 
138 void
cmd_account_enable_shows_usage_when_no_arg(void ** state)139 cmd_account_enable_shows_usage_when_no_arg(void** state)
140 {
141     gchar* args[] = { "enable", NULL };
142 
143     expect_string(cons_bad_cmd_usage, cmd, CMD_ACCOUNT);
144 
145     gboolean result = cmd_account_enable(NULL, CMD_ACCOUNT, args);
146     assert_true(result);
147 }
148 
149 void
cmd_account_enable_enables_account(void ** state)150 cmd_account_enable_enables_account(void** state)
151 {
152     gchar* args[] = { "enable", "account_name", NULL };
153 
154     expect_string(accounts_enable, name, "account_name");
155     will_return(accounts_enable, TRUE);
156 
157     expect_cons_show("Account enabled.");
158     expect_cons_show("");
159 
160     gboolean result = cmd_account_enable(NULL, CMD_ACCOUNT, args);
161     assert_true(result);
162 }
163 
164 void
cmd_account_enable_shows_message_when_account_doesnt_exist(void ** state)165 cmd_account_enable_shows_message_when_account_doesnt_exist(void** state)
166 {
167     gchar* args[] = { "enable", "account_name", NULL };
168 
169     expect_any(accounts_enable, name);
170     will_return(accounts_enable, FALSE);
171 
172     expect_cons_show("No such account: account_name");
173     expect_cons_show("");
174 
175     gboolean result = cmd_account_enable(NULL, CMD_ACCOUNT, args);
176     assert_true(result);
177 }
178 
179 void
cmd_account_disable_shows_usage_when_no_arg(void ** state)180 cmd_account_disable_shows_usage_when_no_arg(void** state)
181 {
182     gchar* args[] = { "disable", NULL };
183 
184     expect_string(cons_bad_cmd_usage, cmd, CMD_ACCOUNT);
185 
186     gboolean result = cmd_account_disable(NULL, CMD_ACCOUNT, args);
187     assert_true(result);
188 }
189 
190 void
cmd_account_disable_disables_account(void ** state)191 cmd_account_disable_disables_account(void** state)
192 {
193     gchar* args[] = { "disable", "account_name", NULL };
194 
195     expect_string(accounts_disable, name, "account_name");
196     will_return(accounts_disable, TRUE);
197 
198     expect_cons_show("Account disabled.");
199     expect_cons_show("");
200 
201     gboolean result = cmd_account_disable(NULL, CMD_ACCOUNT, args);
202     assert_true(result);
203 }
204 
205 void
cmd_account_disable_shows_message_when_account_doesnt_exist(void ** state)206 cmd_account_disable_shows_message_when_account_doesnt_exist(void** state)
207 {
208     gchar* args[] = { "disable", "account_name", NULL };
209 
210     expect_any(accounts_disable, name);
211     will_return(accounts_disable, FALSE);
212 
213     expect_cons_show("No such account: account_name");
214     expect_cons_show("");
215 
216     gboolean result = cmd_account_disable(NULL, CMD_ACCOUNT, args);
217     assert_true(result);
218 }
219 
220 void
cmd_account_rename_shows_usage_when_no_args(void ** state)221 cmd_account_rename_shows_usage_when_no_args(void** state)
222 {
223     gchar* args[] = { "rename", NULL };
224 
225     expect_string(cons_bad_cmd_usage, cmd, CMD_ACCOUNT);
226 
227     gboolean result = cmd_account_rename(NULL, CMD_ACCOUNT, args);
228     assert_true(result);
229 }
230 
231 void
cmd_account_rename_shows_usage_when_one_arg(void ** state)232 cmd_account_rename_shows_usage_when_one_arg(void** state)
233 {
234     gchar* args[] = { "rename", "original_name", NULL };
235 
236     expect_string(cons_bad_cmd_usage, cmd, CMD_ACCOUNT);
237 
238     gboolean result = cmd_account_rename(NULL, CMD_ACCOUNT, args);
239     assert_true(result);
240 }
241 
242 void
cmd_account_rename_renames_account(void ** state)243 cmd_account_rename_renames_account(void** state)
244 {
245     gchar* args[] = { "rename", "original_name", "new_name", NULL };
246 
247     expect_string(accounts_rename, account_name, "original_name");
248     expect_string(accounts_rename, new_name, "new_name");
249     will_return(accounts_rename, TRUE);
250 
251     expect_cons_show("Account renamed.");
252     expect_cons_show("");
253 
254     gboolean result = cmd_account_rename(NULL, CMD_ACCOUNT, args);
255     assert_true(result);
256 }
257 
258 void
cmd_account_rename_shows_message_when_not_renamed(void ** state)259 cmd_account_rename_shows_message_when_not_renamed(void** state)
260 {
261     gchar* args[] = { "rename", "original_name", "new_name", NULL };
262 
263     expect_any(accounts_rename, account_name);
264     expect_any(accounts_rename, new_name);
265     will_return(accounts_rename, FALSE);
266 
267     expect_cons_show("Either account original_name doesn't exist, or account new_name already exists.");
268     expect_cons_show("");
269 
270     gboolean result = cmd_account_rename(NULL, CMD_ACCOUNT, args);
271     assert_true(result);
272 }
273 
274 void
cmd_account_set_shows_usage_when_no_args(void ** state)275 cmd_account_set_shows_usage_when_no_args(void** state)
276 {
277     gchar* args[] = { "set", NULL };
278 
279     expect_string(cons_bad_cmd_usage, cmd, CMD_ACCOUNT);
280 
281     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
282     assert_true(result);
283 }
284 
285 void
cmd_account_set_shows_usage_when_one_arg(void ** state)286 cmd_account_set_shows_usage_when_one_arg(void** state)
287 {
288     gchar* args[] = { "set", "a_account", NULL };
289 
290     expect_string(cons_bad_cmd_usage, cmd, CMD_ACCOUNT);
291 
292     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
293     assert_true(result);
294 }
295 
296 void
cmd_account_set_shows_usage_when_two_args(void ** state)297 cmd_account_set_shows_usage_when_two_args(void** state)
298 {
299     gchar* args[] = { "set", "a_account", "a_property", NULL };
300 
301     expect_string(cons_bad_cmd_usage, cmd, CMD_ACCOUNT);
302 
303     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
304     assert_true(result);
305 }
306 
307 void
cmd_account_set_shows_message_when_account_doesnt_exist(void ** state)308 cmd_account_set_shows_message_when_account_doesnt_exist(void** state)
309 {
310     gchar* args[] = { "set", "a_account", "a_property", "a_value", NULL };
311 
312     expect_string(accounts_account_exists, account_name, "a_account");
313     will_return(accounts_account_exists, FALSE);
314 
315     expect_cons_show("Account a_account doesn't exist");
316     expect_cons_show("");
317 
318     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
319     assert_true(result);
320 }
321 
322 void
cmd_account_set_jid_shows_message_for_malformed_jid(void ** state)323 cmd_account_set_jid_shows_message_for_malformed_jid(void** state)
324 {
325     gchar* args[] = { "set", "a_account", "jid", "@malformed", NULL };
326 
327     expect_any(accounts_account_exists, account_name);
328     will_return(accounts_account_exists, TRUE);
329 
330     expect_cons_show("Malformed jid: @malformed");
331 
332     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
333     assert_true(result);
334 }
335 
336 void
cmd_account_set_jid_sets_barejid(void ** state)337 cmd_account_set_jid_sets_barejid(void** state)
338 {
339     gchar* args[] = { "set", "a_account", "jid", "a_local@a_domain", NULL };
340 
341     expect_any(accounts_account_exists, account_name);
342     will_return(accounts_account_exists, TRUE);
343 
344     expect_string(accounts_set_jid, account_name, "a_account");
345     expect_string(accounts_set_jid, value, "a_local@a_domain");
346 
347     expect_cons_show("Updated jid for account a_account: a_local@a_domain");
348     expect_cons_show("");
349 
350     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
351     assert_true(result);
352 }
353 
354 void
cmd_account_set_jid_sets_resource(void ** state)355 cmd_account_set_jid_sets_resource(void** state)
356 {
357     gchar* args[] = { "set", "a_account", "jid", "a_local@a_domain/a_resource", NULL };
358 
359     expect_any(accounts_account_exists, account_name);
360     will_return(accounts_account_exists, TRUE);
361 
362     expect_string(accounts_set_jid, account_name, "a_account");
363     expect_string(accounts_set_jid, value, "a_local@a_domain");
364 
365     expect_cons_show("Updated jid for account a_account: a_local@a_domain");
366 
367     expect_string(accounts_set_resource, account_name, "a_account");
368     expect_string(accounts_set_resource, value, "a_resource");
369 
370     expect_cons_show("Updated resource for account a_account: a_resource");
371     expect_cons_show("");
372 
373     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
374     assert_true(result);
375 }
376 
377 void
cmd_account_set_server_sets_server(void ** state)378 cmd_account_set_server_sets_server(void** state)
379 {
380     gchar* args[] = { "set", "a_account", "server", "a_server", NULL };
381 
382     expect_any(accounts_account_exists, account_name);
383     will_return(accounts_account_exists, TRUE);
384 
385     expect_string(accounts_set_server, account_name, "a_account");
386     expect_string(accounts_set_server, value, "a_server");
387 
388     expect_cons_show("Updated server for account a_account: a_server");
389     expect_cons_show("");
390 
391     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
392     assert_true(result);
393 }
394 
395 void
cmd_account_set_resource_sets_resource(void ** state)396 cmd_account_set_resource_sets_resource(void** state)
397 {
398     gchar* args[] = { "set", "a_account", "resource", "a_resource", NULL };
399 
400     will_return(connection_get_status, JABBER_DISCONNECTED);
401 
402     expect_any(accounts_account_exists, account_name);
403     will_return(accounts_account_exists, TRUE);
404 
405     expect_string(accounts_set_resource, account_name, "a_account");
406     expect_string(accounts_set_resource, value, "a_resource");
407 
408     expect_cons_show("Updated resource for account a_account: a_resource");
409     expect_cons_show("");
410 
411     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
412     assert_true(result);
413 }
414 
415 void
cmd_account_set_resource_sets_resource_with_online_message(void ** state)416 cmd_account_set_resource_sets_resource_with_online_message(void** state)
417 {
418     gchar* args[] = { "set", "a_account", "resource", "a_resource", NULL };
419 
420     will_return(connection_get_status, JABBER_CONNECTED);
421 
422     expect_any(accounts_account_exists, account_name);
423     will_return(accounts_account_exists, TRUE);
424 
425     expect_string(accounts_set_resource, account_name, "a_account");
426     expect_string(accounts_set_resource, value, "a_resource");
427 
428     expect_cons_show("Updated resource for account a_account: a_resource, reconnect to pick up the change.");
429     expect_cons_show("");
430 
431     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
432     assert_true(result);
433 }
434 
435 void
cmd_account_set_password_sets_password(void ** state)436 cmd_account_set_password_sets_password(void** state)
437 {
438     gchar* args[] = { "set", "a_account", "password", "a_password", NULL };
439     ProfAccount* account = account_new("a_account", NULL, NULL, NULL,
440                                        TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
441 
442     expect_any(accounts_account_exists, account_name);
443     will_return(accounts_account_exists, TRUE);
444 
445     expect_string(accounts_get_account, name, "a_account");
446     will_return(accounts_get_account, account);
447 
448     expect_string(accounts_set_password, account_name, "a_account");
449     expect_string(accounts_set_password, value, "a_password");
450 
451     expect_cons_show("Updated password for account a_account");
452     expect_cons_show("");
453 
454     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
455     assert_true(result);
456 }
457 
458 void
cmd_account_set_eval_password_sets_eval_password(void ** state)459 cmd_account_set_eval_password_sets_eval_password(void** state)
460 {
461     gchar* args[] = { "set", "a_account", "eval_password", "a_password", NULL };
462     ProfAccount* account = account_new("a_account", NULL, NULL, NULL,
463                                        TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
464 
465     expect_any(accounts_account_exists, account_name);
466     will_return(accounts_account_exists, TRUE);
467 
468     expect_string(accounts_get_account, name, "a_account");
469     will_return(accounts_get_account, account);
470 
471     expect_string(accounts_set_eval_password, account_name, "a_account");
472     expect_string(accounts_set_eval_password, value, "a_password");
473 
474     expect_cons_show("Updated eval_password for account a_account");
475     expect_cons_show("");
476 
477     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
478     assert_true(result);
479 }
480 
481 void
cmd_account_set_password_when_eval_password_set(void ** state)482 cmd_account_set_password_when_eval_password_set(void** state)
483 {
484     gchar* args[] = { "set", "a_account", "password", "a_password", NULL };
485     ProfAccount* account = account_new("a_account", NULL, NULL, "a_password",
486                                        TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
487 
488     expect_any(accounts_account_exists, account_name);
489     will_return(accounts_account_exists, TRUE);
490 
491     expect_string(accounts_get_account, name, "a_account");
492     will_return(accounts_get_account, account);
493 
494     expect_cons_show("Cannot set password when eval_password is set.");
495 
496     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
497     assert_true(result);
498 }
499 
500 void
cmd_account_set_eval_password_when_password_set(void ** state)501 cmd_account_set_eval_password_when_password_set(void** state)
502 {
503     gchar* args[] = { "set", "a_account", "eval_password", "a_password", NULL };
504     ProfAccount* account = account_new("a_account", NULL, "a_password", NULL,
505                                        TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
506 
507     expect_any(accounts_account_exists, account_name);
508     will_return(accounts_account_exists, TRUE);
509 
510     expect_string(accounts_get_account, name, "a_account");
511     will_return(accounts_get_account, account);
512 
513     expect_cons_show("Cannot set eval_password when password is set.");
514 
515     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
516     assert_true(result);
517 }
518 
519 void
cmd_account_set_muc_sets_muc(void ** state)520 cmd_account_set_muc_sets_muc(void** state)
521 {
522     gchar* args[] = { "set", "a_account", "muc", "a_muc", NULL };
523 
524     expect_any(accounts_account_exists, account_name);
525     will_return(accounts_account_exists, TRUE);
526 
527     expect_string(accounts_set_muc_service, account_name, "a_account");
528     expect_string(accounts_set_muc_service, value, "a_muc");
529 
530     expect_cons_show("Updated muc service for account a_account: a_muc");
531     expect_cons_show("");
532 
533     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
534     assert_true(result);
535 }
536 
537 void
cmd_account_set_nick_sets_nick(void ** state)538 cmd_account_set_nick_sets_nick(void** state)
539 {
540     gchar* args[] = { "set", "a_account", "nick", "a_nick", NULL };
541 
542     expect_any(accounts_account_exists, account_name);
543     will_return(accounts_account_exists, TRUE);
544 
545     expect_string(accounts_set_muc_nick, account_name, "a_account");
546     expect_string(accounts_set_muc_nick, value, "a_nick");
547 
548     expect_cons_show("Updated muc nick for account a_account: a_nick");
549     expect_cons_show("");
550 
551     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
552     assert_true(result);
553 }
554 
555 void
cmd_account_show_message_for_missing_otr_policy(void ** state)556 cmd_account_show_message_for_missing_otr_policy(void** state)
557 {
558     gchar* args[] = { "set", "a_account", "otr", NULL };
559 
560     expect_string(cons_bad_cmd_usage, cmd, CMD_ACCOUNT);
561 
562     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
563     assert_true(result);
564 }
565 
566 void
cmd_account_show_message_for_invalid_otr_policy(void ** state)567 cmd_account_show_message_for_invalid_otr_policy(void** state)
568 {
569     gchar* args[] = { "set", "a_account", "otr", "bad_otr_policy", NULL };
570 
571     expect_any(accounts_account_exists, account_name);
572     will_return(accounts_account_exists, TRUE);
573 
574     expect_cons_show("OTR policy must be one of: manual, opportunistic or always.");
575 
576     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
577     assert_true(result);
578 }
579 
580 void
cmd_account_set_otr_sets_otr(void ** state)581 cmd_account_set_otr_sets_otr(void** state)
582 {
583     gchar* args[] = { "set", "a_account", "otr", "opportunistic", NULL };
584 
585     expect_any(accounts_account_exists, account_name);
586     will_return(accounts_account_exists, TRUE);
587 
588     expect_string(accounts_set_otr_policy, account_name, "a_account");
589     expect_string(accounts_set_otr_policy, value, "opportunistic");
590 
591     expect_cons_show("Updated OTR policy for account a_account: opportunistic");
592     expect_cons_show("");
593 
594     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
595     assert_true(result);
596 }
597 
598 void
cmd_account_set_status_shows_message_when_invalid_status(void ** state)599 cmd_account_set_status_shows_message_when_invalid_status(void** state)
600 {
601     gchar* args[] = { "set", "a_account", "status", "bad_status", NULL };
602 
603     expect_any(accounts_account_exists, account_name);
604     will_return(accounts_account_exists, TRUE);
605 
606     expect_cons_show("Invalid status: bad_status");
607     expect_cons_show("");
608 
609     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
610     assert_true(result);
611 }
612 
613 void
cmd_account_set_status_sets_status_when_valid(void ** state)614 cmd_account_set_status_sets_status_when_valid(void** state)
615 {
616     gchar* args[] = { "set", "a_account", "status", "away", NULL };
617 
618     expect_any(accounts_account_exists, account_name);
619     will_return(accounts_account_exists, TRUE);
620 
621     expect_string(accounts_set_login_presence, account_name, "a_account");
622     expect_string(accounts_set_login_presence, value, "away");
623 
624     expect_cons_show("Updated login status for account a_account: away");
625     expect_cons_show("");
626 
627     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
628     assert_true(result);
629 }
630 
631 void
cmd_account_set_status_sets_status_when_last(void ** state)632 cmd_account_set_status_sets_status_when_last(void** state)
633 {
634     gchar* args[] = { "set", "a_account", "status", "last", NULL };
635 
636     expect_any(accounts_account_exists, account_name);
637     will_return(accounts_account_exists, TRUE);
638 
639     expect_string(accounts_set_login_presence, account_name, "a_account");
640     expect_string(accounts_set_login_presence, value, "last");
641 
642     expect_cons_show("Updated login status for account a_account: last");
643     expect_cons_show("");
644 
645     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
646     assert_true(result);
647 }
648 
649 void
cmd_account_set_invalid_presence_string_priority_shows_message(void ** state)650 cmd_account_set_invalid_presence_string_priority_shows_message(void** state)
651 {
652     gchar* args[] = { "set", "a_account", "blah", "10", NULL };
653 
654     expect_any(accounts_account_exists, account_name);
655     will_return(accounts_account_exists, TRUE);
656 
657     expect_cons_show("Invalid property: blah");
658     expect_cons_show("");
659 
660     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
661     assert_true(result);
662 }
663 
664 void
cmd_account_set_last_priority_shows_message(void ** state)665 cmd_account_set_last_priority_shows_message(void** state)
666 {
667     gchar* args[] = { "set", "a_account", "last", "10", NULL };
668 
669     expect_any(accounts_account_exists, account_name);
670     will_return(accounts_account_exists, TRUE);
671 
672     expect_cons_show("Invalid property: last");
673     expect_cons_show("");
674 
675     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
676     assert_true(result);
677 }
678 
679 void
cmd_account_set_online_priority_sets_preference(void ** state)680 cmd_account_set_online_priority_sets_preference(void** state)
681 {
682     gchar* args[] = { "set", "a_account", "online", "10", NULL };
683 
684     expect_any(accounts_account_exists, account_name);
685     will_return(accounts_account_exists, TRUE);
686 
687     expect_string(accounts_set_priority_online, account_name, "a_account");
688     expect_value(accounts_set_priority_online, value, 10);
689 
690     will_return(connection_get_status, JABBER_DISCONNECTED);
691 
692     expect_cons_show("Updated online priority for account a_account: 10");
693     expect_cons_show("");
694 
695     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
696     assert_true(result);
697 }
698 
699 void
cmd_account_set_chat_priority_sets_preference(void ** state)700 cmd_account_set_chat_priority_sets_preference(void** state)
701 {
702     gchar* args[] = { "set", "a_account", "chat", "10", NULL };
703 
704     expect_any(accounts_account_exists, account_name);
705     will_return(accounts_account_exists, TRUE);
706 
707     expect_string(accounts_set_priority_chat, account_name, "a_account");
708     expect_value(accounts_set_priority_chat, value, 10);
709 
710     will_return(connection_get_status, JABBER_DISCONNECTED);
711 
712     expect_cons_show("Updated chat priority for account a_account: 10");
713     expect_cons_show("");
714 
715     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
716     assert_true(result);
717 }
718 
719 void
cmd_account_set_away_priority_sets_preference(void ** state)720 cmd_account_set_away_priority_sets_preference(void** state)
721 {
722     gchar* args[] = { "set", "a_account", "away", "10", NULL };
723 
724     expect_any(accounts_account_exists, account_name);
725     will_return(accounts_account_exists, TRUE);
726 
727     expect_string(accounts_set_priority_away, account_name, "a_account");
728     expect_value(accounts_set_priority_away, value, 10);
729 
730     will_return(connection_get_status, JABBER_DISCONNECTED);
731 
732     expect_cons_show("Updated away priority for account a_account: 10");
733     expect_cons_show("");
734 
735     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
736     assert_true(result);
737 }
738 
739 void
cmd_account_set_xa_priority_sets_preference(void ** state)740 cmd_account_set_xa_priority_sets_preference(void** state)
741 {
742     gchar* args[] = { "set", "a_account", "xa", "10", NULL };
743 
744     expect_any(accounts_account_exists, account_name);
745     will_return(accounts_account_exists, TRUE);
746 
747     expect_string(accounts_set_priority_xa, account_name, "a_account");
748     expect_value(accounts_set_priority_xa, value, 10);
749 
750     will_return(connection_get_status, JABBER_DISCONNECTED);
751 
752     expect_cons_show("Updated xa priority for account a_account: 10");
753     expect_cons_show("");
754 
755     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
756     assert_true(result);
757 }
758 
759 void
cmd_account_set_dnd_priority_sets_preference(void ** state)760 cmd_account_set_dnd_priority_sets_preference(void** state)
761 {
762     gchar* args[] = { "set", "a_account", "dnd", "10", NULL };
763 
764     expect_any(accounts_account_exists, account_name);
765     will_return(accounts_account_exists, TRUE);
766 
767     expect_string(accounts_set_priority_dnd, account_name, "a_account");
768     expect_value(accounts_set_priority_dnd, value, 10);
769 
770     will_return(connection_get_status, JABBER_DISCONNECTED);
771 
772     expect_cons_show("Updated dnd priority for account a_account: 10");
773     expect_cons_show("");
774 
775     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
776     assert_true(result);
777 }
778 
779 void
cmd_account_set_priority_too_low_shows_message(void ** state)780 cmd_account_set_priority_too_low_shows_message(void** state)
781 {
782     gchar* args[] = { "set", "a_account", "online", "-150", NULL };
783 
784     expect_any(accounts_account_exists, account_name);
785     will_return(accounts_account_exists, TRUE);
786 
787     expect_cons_show("Value -150 out of range. Must be in -128..127.");
788 
789     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
790     assert_true(result);
791 }
792 
793 void
cmd_account_set_priority_too_high_shows_message(void ** state)794 cmd_account_set_priority_too_high_shows_message(void** state)
795 {
796     gchar* args[] = { "set", "a_account", "online", "150", NULL };
797 
798     expect_any(accounts_account_exists, account_name);
799     will_return(accounts_account_exists, TRUE);
800 
801     expect_cons_show("Value 150 out of range. Must be in -128..127.");
802 
803     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
804     assert_true(result);
805 }
806 
807 void
cmd_account_set_priority_when_not_number_shows_message(void ** state)808 cmd_account_set_priority_when_not_number_shows_message(void** state)
809 {
810     gchar* args[] = { "set", "a_account", "online", "abc", NULL };
811 
812     expect_any(accounts_account_exists, account_name);
813     will_return(accounts_account_exists, TRUE);
814 
815     expect_cons_show("Could not convert \"abc\" to a number.");
816 
817     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
818     assert_true(result);
819 }
820 
821 void
cmd_account_set_priority_when_empty_shows_message(void ** state)822 cmd_account_set_priority_when_empty_shows_message(void** state)
823 {
824     gchar* args[] = { "set", "a_account", "online", "", NULL };
825 
826     expect_any(accounts_account_exists, account_name);
827     will_return(accounts_account_exists, TRUE);
828 
829     expect_cons_show("Could not convert \"\" to a number.");
830 
831     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
832     assert_true(result);
833 }
834 
835 void
cmd_account_set_priority_updates_presence_when_account_connected_with_presence(void ** state)836 cmd_account_set_priority_updates_presence_when_account_connected_with_presence(void** state)
837 {
838     gchar* args[] = { "set", "a_account", "online", "10", NULL };
839 
840     expect_any(accounts_account_exists, account_name);
841     will_return(accounts_account_exists, TRUE);
842 
843     expect_any(accounts_set_priority_online, account_name);
844     expect_any(accounts_set_priority_online, value);
845 
846     will_return(connection_get_status, JABBER_CONNECTED);
847 
848     expect_any(accounts_get_last_presence, account_name);
849     will_return(accounts_get_last_presence, RESOURCE_ONLINE);
850 
851     will_return(session_get_account_name, "a_account");
852 
853 #ifdef HAVE_LIBGPGME
854     ProfAccount* account = account_new("a_account", "a_jid", NULL, NULL, TRUE, NULL, 5222, "a_resource",
855                                        NULL, NULL, 10, 10, 10, 10, 10, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
856 
857     will_return(session_get_account_name, "a_account");
858     expect_any(accounts_get_account, name);
859     will_return(accounts_get_account, account);
860 #endif
861     expect_value(presence_send, status, RESOURCE_ONLINE);
862     expect_value(presence_send, idle, 0);
863     expect_value(presence_send, signed_status, NULL);
864 
865     expect_cons_show("Updated online priority for account a_account: 10");
866     expect_cons_show("");
867 
868     gboolean result = cmd_account_set(NULL, CMD_ACCOUNT, args);
869     assert_true(result);
870 }
871 
872 void
cmd_account_clear_shows_usage_when_no_args(void ** state)873 cmd_account_clear_shows_usage_when_no_args(void** state)
874 {
875     gchar* args[] = { "clear", NULL };
876 
877     expect_string(cons_bad_cmd_usage, cmd, CMD_ACCOUNT);
878 
879     gboolean result = cmd_account_clear(NULL, CMD_ACCOUNT, args);
880     assert_true(result);
881 }
882 
883 void
cmd_account_clear_shows_usage_when_one_arg(void ** state)884 cmd_account_clear_shows_usage_when_one_arg(void** state)
885 {
886     gchar* args[] = { "clear", "a_account", NULL };
887 
888     expect_string(cons_bad_cmd_usage, cmd, CMD_ACCOUNT);
889 
890     gboolean result = cmd_account_clear(NULL, CMD_ACCOUNT, args);
891     assert_true(result);
892 }
893 
894 void
cmd_account_clear_shows_message_when_account_doesnt_exist(void ** state)895 cmd_account_clear_shows_message_when_account_doesnt_exist(void** state)
896 {
897     gchar* args[] = { "clear", "a_account", "a_property", NULL };
898 
899     expect_string(accounts_account_exists, account_name, "a_account");
900     will_return(accounts_account_exists, FALSE);
901 
902     expect_cons_show("Account a_account doesn't exist");
903     expect_cons_show("");
904 
905     gboolean result = cmd_account_clear(NULL, CMD_ACCOUNT, args);
906     assert_true(result);
907 }
908 
909 void
cmd_account_clear_shows_message_when_invalid_property(void ** state)910 cmd_account_clear_shows_message_when_invalid_property(void** state)
911 {
912     gchar* args[] = { "clear", "a_account", "badproperty", NULL };
913 
914     expect_any(accounts_account_exists, account_name);
915     will_return(accounts_account_exists, TRUE);
916 
917     expect_cons_show("Invalid property: badproperty");
918     expect_cons_show("");
919 
920     gboolean result = cmd_account_clear(NULL, CMD_ACCOUNT, args);
921     assert_true(result);
922 }
923