about summary refs log tree commit diff
path: root/nixos/tests/gitea.nix
blob: 86d4fce379291f154f58f63ede86ee6ad09abde3 (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
{ system ? builtins.currentSystem,
  config ? {},
  giteaPackage ? pkgs.gitea,
  pkgs ? import ../.. { inherit system config; }
}:

with import ../lib/testing-python.nix { inherit system pkgs; };
with pkgs.lib;

let
  ## gpg --faked-system-time='20230301T010000!' --quick-generate-key snakeoil ed25519 sign
  signingPrivateKey = ''
    -----BEGIN PGP PRIVATE KEY BLOCK-----

    lFgEY/6jkBYJKwYBBAHaRw8BAQdADXiZRV8RJUyC9g0LH04wLMaJL9WTc+szbMi7
    5fw4yP8AAQCl8EwGfzSLm/P6fCBfA3I9znFb3MEHGCCJhJ6VtKYyRw7ktAhzbmFr
    ZW9pbIiUBBMWCgA8FiEE+wUM6VW/NLtAdSixTWQt6LZ4x50FAmP+o5ACGwMFCQPC
    ZwAECwkIBwQVCgkIBRYCAwEAAh4FAheAAAoJEE1kLei2eMedFTgBAKQs1oGFZrCI
    TZP42hmBTKxGAI1wg7VSdDEWTZxut/2JAQDGgo2sa4VHMfj0aqYGxrIwfP2B7JHO
    GCqGCRf9O/hzBA==
    =9Uy3
    -----END PGP PRIVATE KEY BLOCK-----
  '';
  signingPrivateKeyId = "4D642DE8B678C79D";

  supportedDbTypes = [ "mysql" "postgres" "sqlite3" ];
  makeGiteaTest = type: nameValuePair type (makeTest {
    name = "${giteaPackage.pname}-${type}";
    meta.maintainers = with maintainers; [ aanderse indeednotjames kolaente ma27 ];

    nodes = {
      server = { config, pkgs, ... }: {
        virtualisation.memorySize = 2048;
        services.gitea = {
          enable = true;
          database = { inherit type; };
          package = giteaPackage;
          settings.service.DISABLE_REGISTRATION = true;
          settings."repository.signing".SIGNING_KEY = signingPrivateKeyId;
        };
        environment.systemPackages = [ giteaPackage pkgs.gnupg pkgs.jq ];
        services.openssh.enable = true;
      };
      client1 = { config, pkgs, ... }: {
        environment.systemPackages = [ pkgs.git ];
      };
      client2 = { config, pkgs, ... }: {
        environment.systemPackages = [ pkgs.git ];
      };
    };

    testScript = let
      inherit (import ./ssh-keys.nix pkgs) snakeOilPrivateKey snakeOilPublicKey;
    in ''
      GIT_SSH_COMMAND = "ssh -i $HOME/.ssh/privk -o StrictHostKeyChecking=no"
      REPO = "gitea@server:test/repo"
      PRIVK = "${snakeOilPrivateKey}"

      start_all()

      client1.succeed("mkdir /tmp/repo")
      client1.succeed("mkdir -p $HOME/.ssh")
      client1.succeed(f"cat {PRIVK} > $HOME/.ssh/privk")
      client1.succeed("chmod 0400 $HOME/.ssh/privk")
      client1.succeed("git -C /tmp/repo init")
      client1.succeed("echo hello world > /tmp/repo/testfile")
      client1.succeed("git -C /tmp/repo add .")
      client1.succeed("git config --global user.email test@localhost")
      client1.succeed("git config --global user.name test")
      client1.succeed("git -C /tmp/repo commit -m 'Initial import'")
      client1.succeed(f"git -C /tmp/repo remote add origin {REPO}")

      server.wait_for_unit("gitea.service")
      server.wait_for_open_port(3000)
      server.succeed("curl --fail http://localhost:3000/")

      server.succeed(
          "su -l gitea -c 'gpg --homedir /var/lib/gitea/data/home/.gnupg "
          + "--import ${toString (pkgs.writeText "gitea.key" signingPrivateKey)}'"
      )

      assert "BEGIN PGP PUBLIC KEY BLOCK" in server.succeed("curl http://localhost:3000/api/v1/signing-key.gpg")

      server.succeed(
          "curl --fail http://localhost:3000/user/sign_up | grep 'Registration is disabled. "
          + "Please contact your site administrator.'"
      )
      server.succeed(
          "su -l gitea -c 'GITEA_WORK_DIR=/var/lib/gitea gitea admin user create "
          + "--username test --password totallysafe --email test@localhost'"
      )

      api_token = server.succeed(
          "curl --fail -X POST http://test:totallysafe@localhost:3000/api/v1/users/test/tokens "
          + "-H 'Accept: application/json' -H 'Content-Type: application/json' -d "
          + "'{\"name\":\"token\",\"scopes\":[\"all\"]}' | jq '.sha1' | xargs echo -n"
      )

      server.succeed(
          "curl --fail -X POST http://localhost:3000/api/v1/user/repos "
          + "-H 'Accept: application/json' -H 'Content-Type: application/json' "
          + f"-H 'Authorization: token {api_token}'"
          + ' -d \'{"auto_init":false, "description":"string", "license":"mit", "name":"repo", "private":false}\'''
      )

      server.succeed(
          "curl --fail -X POST http://localhost:3000/api/v1/user/keys "
          + "-H 'Accept: application/json' -H 'Content-Type: application/json' "
          + f"-H 'Authorization: token {api_token}'"
          + ' -d \'{"key":"${snakeOilPublicKey}","read_only":true,"title":"SSH"}\'''
      )

      client1.succeed(
          f"GIT_SSH_COMMAND='{GIT_SSH_COMMAND}' git -C /tmp/repo push origin master"
      )

      client2.succeed("mkdir -p $HOME/.ssh")
      client2.succeed(f"cat {PRIVK} > $HOME/.ssh/privk")
      client2.succeed("chmod 0400 $HOME/.ssh/privk")
      client2.succeed(f"GIT_SSH_COMMAND='{GIT_SSH_COMMAND}' git clone {REPO}")
      client2.succeed('test "$(cat repo/testfile | xargs echo -n)" = "hello world"')

      server.succeed(
          'test "$(curl http://localhost:3000/api/v1/repos/test/repo/commits '
          + '-H "Accept: application/json" | jq length)" = "1"'
      )

      client1.shutdown()
      client2.shutdown()
      server.shutdown()
    '';
  });
in

listToAttrs (map makeGiteaTest supportedDbTypes)