about summary refs log tree commit diff stats
path: root/tests/test_cmd_roster.c
blob: e59f5e5e51bf93bd396dbc75a85a85806225cf37 (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
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

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

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

#include "roster_list.h"
#include "command/commands.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 not currently connected.");

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

    free(help);
}

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

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

void cmd_roster_shows_message_when_disconnected(void **state)
{
    test_with_connection_status(JABBER_DISCONNECTED);
}

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

void cmd_roster_shows_roster_when_no_args(void **state)
{
    mock_cons_show_roster();
    CommandHelp *help = malloc(sizeof(CommandHelp));
    gchar *args[] = { NULL };

    mock_connection_status(JABBER_CONNECTED);
    roster_init();
    roster_add("bob@server.org", "bob", NULL, "both", FALSE);
    GSList *roster = roster_get_contacts();
    cons_show_roster_expect(roster);

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

    free(help);
    roster_free();
}

void cmd_roster_add_shows_message_when_no_jid(void **state)
{
    mock_cons_show();
    CommandHelp *help = malloc(sizeof(CommandHelp));
    help->usage = "some usage";
    gchar *args[] = { "add", NULL };

    mock_connection_status(JABBER_CONNECTED);
    expect_cons_show("Usage: some usage");

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

    free(help);
}

void cmd_roster_add_sends_roster_add_request(void **state)
{
    char *jid = "bob@server.org";
    char *nick = "bob";
    CommandHelp *help = malloc(sizeof(CommandHelp));
    gchar *args[] = { "add", jid, nick, NULL };

    mock_roster_send_add_new();
    mock_connection_status(JABBER_CONNECTED);
    roster_send_add_new_expect(jid, nick);

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

    free(help);
}

void cmd_roster_remove_shows_message_when_no_jid(void **state)
{
    mock_cons_show();
    CommandHelp *help = malloc(sizeof(CommandHelp));
    help->usage = "some usage";
    gchar *args[] = { "remove", NULL };

    mock_connection_status(JABBER_CONNECTED);
    expect_cons_show("Usage: some usage");

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

    free(help);
}

void cmd_roster_remove_sends_roster_remove_request(void)
{
    char *jid = "bob@server.org";
    CommandHelp *help = malloc(sizeof(CommandHelp));
    gchar *args[] = { "remove", jid, NULL };

    mock_roster_send_remove();
    mock_connection_status(JABBER_CONNECTED);
    roster_send_remove_expect(jid);

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

    free(help);
}