mvnd: fix eval (#373133)
[NixPkgs.git] / nixos / tests / matrix / conduwuit.nix
blob3e1123b692a6cb3326355d4b339d29204f89b943
1 { lib, ... }:
2 let
3   name = "conduwuit";
4 in
6   inherit name;
8   nodes = {
9     conduwuit = {
10       services.conduwuit = {
11         enable = true;
12         settings.global = {
13           server_name = name;
14           address = [ "0.0.0.0" ];
15           allow_registration = true;
16           yes_i_am_very_very_sure_i_want_an_open_registration_server_prone_to_abuse = true;
17         };
18         extraEnvironment.RUST_BACKTRACE = "yes";
19       };
20       networking.firewall.allowedTCPPorts = [ 6167 ];
21     };
22     client =
23       { pkgs, ... }:
24       {
25         environment.systemPackages = [
26           (pkgs.writers.writePython3Bin "do_test" { libraries = [ pkgs.python3Packages.matrix-nio ]; } ''
27             import asyncio
28             import nio
31             async def main() -> None:
32                 # Connect to conduwuit
33                 client = nio.AsyncClient("http://conduwuit:6167", "alice")
35                 # Register as user alice
36                 response = await client.register("alice", "my-secret-password")
38                 # Log in as user alice
39                 response = await client.login("my-secret-password")
41                 # Create a new room
42                 response = await client.room_create(federate=False)
43                 print("Matrix room create response:", response)
44                 assert isinstance(response, nio.RoomCreateResponse)
45                 room_id = response.room_id
47                 # Join the room
48                 response = await client.join(room_id)
49                 print("Matrix join response:", response)
50                 assert isinstance(response, nio.JoinResponse)
52                 # Send a message to the room
53                 response = await client.room_send(
54                     room_id=room_id,
55                     message_type="m.room.message",
56                     content={
57                         "msgtype": "m.text",
58                         "body": "Hello conduwuit!"
59                     }
60                 )
61                 print("Matrix room send response:", response)
62                 assert isinstance(response, nio.RoomSendResponse)
64                 # Sync responses
65                 response = await client.sync(timeout=30000)
66                 print("Matrix sync response:", response)
67                 assert isinstance(response, nio.SyncResponse)
69                 # Check the message was received by conduwuit
70                 last_message = response.rooms.join[room_id].timeline.events[-1].body
71                 assert last_message == "Hello conduwuit!"
73                 # Leave the room
74                 response = await client.room_leave(room_id)
75                 print("Matrix room leave response:", response)
76                 assert isinstance(response, nio.RoomLeaveResponse)
78                 # Close the client
79                 await client.close()
82             if __name__ == "__main__":
83                 asyncio.run(main())
84           '')
85         ];
86       };
87   };
89   testScript = ''
90     start_all()
92     with subtest("start conduwuit"):
93           conduwuit.wait_for_unit("conduwuit.service")
94           conduwuit.wait_for_open_port(6167)
96     with subtest("ensure messages can be exchanged"):
97           client.succeed("do_test >&2")
98   '';
100   meta.maintainers = with lib.maintainers; [
101     niklaskorz
102   ];