summaryrefslogtreecommitdiff
path: root/test.nix
blob: 0257623169cce4308ba59573b1377be424074818 (plain)
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
{ pkgs ? import <nixpkgs> { } }:
(import "${pkgs.path}/nixos/tests/make-test-python.nix" (
  { pkgs, ... }:
    let
      homeserverURL = "http://homeserver:8008";
      homeserverDomain = "example.com";

      private_key = pkgs.runCommand "matrix_key.pem" {
        buildInputs = [ pkgs.dendrite ];
      } "generate-keys --private-key $out";
    in
      {
        name = "dendrite";
        meta = with pkgs.lib; {
          maintainers = teams.matrix.members;
        };

        nodes = {
          homeserver = { pkgs, ... }: {
            imports = [ ./module ];
            services.dendrite = {
              enable = true;
              settings = {
                global.server_name = homeserverDomain;
                global.private_key = private_key;
                client_api.registration_disabled = false;
              };
            };

            networking.firewall.allowedTCPPorts = [ 8008 ];

            services.matrix-appservices = {
              inherit homeserverDomain homeserverURL;
              addRegistrationFiles = true;
              homeserver = "dendrite";
              services = {
                discord = {
                  port = 29180;
                  package = pkgs.mx-puppet-discord;
                  format = "mx-puppet";
                };
              };
            };
          };

          client = { pkgs, ... }: {
            environment.systemPackages = [
              (
                pkgs.writers.writePython3Bin "do_test"
                  { libraries = [ pkgs.python3Packages.matrix-nio ]; } ''
                  import asyncio

                  from nio import AsyncClient


                  async def main() -> None:
                      # Connect to dendrite
                      client = AsyncClient("http://homeserver:8008", "alice")

                      # Register as user alice
                      response = await client.register("alice", "my-secret-password")

                      # Log in as user alice
                      response = await client.login("my-secret-password")

                      # Create a new room
                      response = await client.room_create(federate=False)
                      room_id = response.room_id

                      # Join the room
                      response = await client.join(room_id)

                      # Invite whatsapp user to room
                      response = await client.room_invite(
                        room_id,
                        "@_discordpuppet_bot:${homeserverDomain}"
                      )

                      # Send a message to the room
                      response = await client.room_send(
                          room_id=room_id,
                          message_type="m.room.message",
                          content={
                              "msgtype": "m.text",
                              "body": "ping"
                          }
                      )

                      # Sync responses
                      response = await client.sync(timeout=30000)

                      response = await client.joined_members(room_id)

                      # Check the message was received by dendrite
                      # last_message = response.rooms.join[room_id].timeline.events[-1].body

                      # Leave the room
                      response = await client.room_leave(room_id)

                      # Close the client
                      await client.close()

                  asyncio.get_event_loop().run_until_complete(main())
                ''
              )
            ];
          };
        };

        testScript = ''
          start_all()

          with subtest("start the homeserver"):
              homeserver.wait_for_unit("dendrite.service")
              homeserver.wait_for_open_port(8008)
              homeserver.wait_for_unit("matrix-as-discord.service")

          with subtest("ensure messages can be exchanged"):
              client.succeed("do_test")
        '';

      }
)) { inherit (pkgs) pkgs system; }