python310Packages.pydeconz: 104 -> 105
[NixPkgs.git] / nixos / tests / matrix / dendrite.nix
blob82e71d912130dc17503a9f3779c31e716dd15e99
1 import ../make-test-python.nix (
2   { pkgs, ... }:
3     let
4       homeserverUrl = "http://homeserver:8008";
6       private_key = pkgs.runCommand "matrix_key.pem" {
7         buildInputs = [ pkgs.dendrite ];
8       } "generate-keys --private-key $out";
9     in
10       {
11         name = "dendrite";
12         meta = with pkgs.lib; {
13           maintainers = teams.matrix.members;
14         };
16         nodes = {
17           homeserver = { pkgs, ... }: {
18             services.dendrite = {
19               enable = true;
20               loadCredential = [ "test_private_key:${private_key}" ];
21               openRegistration = true;
22               settings = {
23                 global.server_name = "test-dendrite-server.com";
24                 global.private_key = "$CREDENTIALS_DIRECTORY/test_private_key";
25                 client_api.registration_disabled = false;
26               };
27             };
29             networking.firewall.allowedTCPPorts = [ 8008 ];
30           };
32           client = { pkgs, ... }: {
33             environment.systemPackages = [
34               (
35                 pkgs.writers.writePython3Bin "do_test"
36                   { libraries = [ pkgs.python3Packages.matrix-nio ]; } ''
37                   import asyncio
39                   from nio import AsyncClient
42                   async def main() -> None:
43                       # Connect to dendrite
44                       client = AsyncClient("http://homeserver:8008", "alice")
46                       # Register as user alice
47                       response = await client.register("alice", "my-secret-password")
49                       # Log in as user alice
50                       response = await client.login("my-secret-password")
52                       # Create a new room
53                       response = await client.room_create(federate=False)
54                       room_id = response.room_id
56                       # Join the room
57                       response = await client.join(room_id)
59                       # Send a message to the room
60                       response = await client.room_send(
61                           room_id=room_id,
62                           message_type="m.room.message",
63                           content={
64                               "msgtype": "m.text",
65                               "body": "Hello world!"
66                           }
67                       )
69                       # Sync responses
70                       response = await client.sync(timeout=30000)
72                       # Check the message was received by dendrite
73                       last_message = response.rooms.join[room_id].timeline.events[-1].body
74                       assert last_message == "Hello world!"
76                       # Leave the room
77                       response = await client.room_leave(room_id)
79                       # Close the client
80                       await client.close()
82                   asyncio.get_event_loop().run_until_complete(main())
83                 ''
84               )
85             ];
86           };
87         };
89         testScript = ''
90           start_all()
92           with subtest("start the homeserver"):
93               homeserver.wait_for_unit("dendrite.service")
94               homeserver.wait_for_open_port(8008)
96           with subtest("ensure messages can be exchanged"):
97               client.succeed("do_test")
98         '';
100       }