Fix routing of outbound iq stanzas
[prosody.git] / tests / test_core_stanza_router.lua
1
2 function core_process_stanza(core_process_stanza)
3         local s2sout_session = { to_host = "remotehost", from_host = "localhost", type = "s2sout" }
4         local local_host_session = { host = "localhost", type = "local" }
5         local local_user_session = { username = "user", host = "localhost", resource = "resource", full_jid = "user@localhost/resource", type = "c2s" }
6         local hosts = {
7                         ["localhost"] = local_host_session;
8                         }
9                                 
10         -- Test message routing
11         local function test_message_full_jid()
12                 local env = testlib_new_env();
13                 local msg = stanza.stanza("message", { to = "user@localhost/resource", type = "chat" }):tag("body"):text("Hello world");
14                 
15                 local target_routed;
16                 
17                 function env.core_route_stanza(p_origin, p_stanza)
18                         assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct");
19                         assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print());
20                         target_routed = true;
21                 end
22                 env.hosts = hosts;
23                 setfenv(core_process_stanza, env);
24                 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
25                 assert_equal(target_routed, true, "stanza was not routed successfully");
26         end
27
28         local function test_message_bare_jid()
29                 local env = testlib_new_env();
30                 local msg = stanza.stanza("message", { to = "user@localhost", type = "chat" }):tag("body"):text("Hello world");
31                 
32                 local target_routed;
33                 
34                 function env.core_route_stanza(p_origin, p_stanza)
35                         assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct");
36                         assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print());
37                         target_routed = true;
38                 end
39                 env.hosts = hosts;
40                 setfenv(core_process_stanza, env);
41                 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
42                 assert_equal(target_routed, true, "stanza was not routed successfully");
43         end
44
45         local function test_message_no_to()
46                 local env = testlib_new_env();
47                 local msg = stanza.stanza("message", { type = "chat" }):tag("body"):text("Hello world");
48                 
49                 local target_handled;
50                 
51                 function env.core_route_stanza(p_origin, p_stanza)
52                 end
53
54                 function env.core_handle_stanza(p_origin, p_stanza)
55                         assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
56                         assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
57                         target_handled = true;          
58                 end
59                 env.hosts = hosts;
60                 setfenv(core_process_stanza, env);
61                 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
62                 assert_equal(target_handled, true, "stanza was not handled successfully");
63         end
64
65         local function test_message_to_remote_bare()
66                 local env = testlib_new_env();
67                 local msg = stanza.stanza("message", { to = "user@remotehost", type = "chat" }):tag("body"):text("Hello world");
68                 
69                 local target_routed;
70                 
71                 function env.core_route_stanza(p_origin, p_stanza)
72                         assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
73                         assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
74                         target_routed = true;           
75                 end
76
77                 env.hosts = hosts;
78                 setfenv(core_process_stanza, env);
79                 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
80                 assert_equal(target_routed, true, "stanza was not routed successfully");
81         end
82
83         local function test_message_to_remote_server()
84                 local env = testlib_new_env();
85                 local msg = stanza.stanza("message", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world");
86                 
87                 local target_routed;
88                 
89                 function env.core_route_stanza(p_origin, p_stanza)
90                         assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
91                         assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
92                         target_routed = true;           
93                 end
94
95                 env.hosts = hosts;
96                 setfenv(core_process_stanza, env);
97                 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
98                 assert_equal(target_routed, true, "stanza was not routed successfully");
99         end
100
101         --IQ tests
102
103
104         local function test_iq_to_remote_server()
105                 local env = testlib_new_env();
106                 local msg = stanza.stanza("iq", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world");
107                 
108                 local target_routed;
109                 
110                 function env.core_route_stanza(p_origin, p_stanza)
111                         assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
112                         assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
113                         target_routed = true;           
114                 end
115
116                 function env.core_handle_stanza(p_origin, p_stanza)
117                         
118                 end
119
120                 env.hosts = hosts;
121                 setfenv(core_process_stanza, env);
122                 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
123                 assert_equal(target_routed, true, "stanza was not routed successfully");
124         end
125
126         runtest(test_message_full_jid, "Messages with full JID destinations get routed");
127         runtest(test_message_bare_jid, "Messages with bare JID destinations get routed");
128         runtest(test_message_no_to, "Messages with no destination are handled by the server");
129         runtest(test_message_to_remote_bare, "Messages to a remote user are routed by the server");
130         runtest(test_message_to_remote_server, "Messages to a remote server's JID are routed");
131
132         runtest(test_iq_to_remote_server, "iq to a remote server's JID are routed");
133
134 end