1{ system ? builtins.currentSystem,
2 config ? {},
3 giteaPackage ? pkgs.gitea,
4 pkgs ? import ../.. { inherit system config; }
5}:
6
7with import ../lib/testing-python.nix { inherit system pkgs; };
8with pkgs.lib;
9
10let
11 ## gpg --faked-system-time='20230301T010000!' --quick-generate-key snakeoil ed25519 sign
12 signingPrivateKey = ''
13 -----BEGIN PGP PRIVATE KEY BLOCK-----
14
15 lFgEY/6jkBYJKwYBBAHaRw8BAQdADXiZRV8RJUyC9g0LH04wLMaJL9WTc+szbMi7
16 5fw4yP8AAQCl8EwGfzSLm/P6fCBfA3I9znFb3MEHGCCJhJ6VtKYyRw7ktAhzbmFr
17 ZW9pbIiUBBMWCgA8FiEE+wUM6VW/NLtAdSixTWQt6LZ4x50FAmP+o5ACGwMFCQPC
18 ZwAECwkIBwQVCgkIBRYCAwEAAh4FAheAAAoJEE1kLei2eMedFTgBAKQs1oGFZrCI
19 TZP42hmBTKxGAI1wg7VSdDEWTZxut/2JAQDGgo2sa4VHMfj0aqYGxrIwfP2B7JHO
20 GCqGCRf9O/hzBA==
21 =9Uy3
22 -----END PGP PRIVATE KEY BLOCK-----
23 '';
24 signingPrivateKeyId = "4D642DE8B678C79D";
25
26 supportedDbTypes = [ "mysql" "postgres" "sqlite3" ];
27 makeGiteaTest = type: nameValuePair type (makeTest {
28 name = "${giteaPackage.pname}-${type}";
29 meta.maintainers = with maintainers; [ aanderse emilylange kolaente ma27 ];
30
31 nodes = {
32 server = { config, pkgs, ... }: {
33 virtualisation.memorySize = 2047;
34 services.gitea = {
35 enable = true;
36 database = { inherit type; };
37 package = giteaPackage;
38 settings.service.DISABLE_REGISTRATION = true;
39 settings."repository.signing".SIGNING_KEY = signingPrivateKeyId;
40 };
41 environment.systemPackages = [ giteaPackage pkgs.gnupg pkgs.jq ];
42 services.openssh.enable = true;
43 };
44 client1 = { config, pkgs, ... }: {
45 environment.systemPackages = [ pkgs.git ];
46 };
47 client2 = { config, pkgs, ... }: {
48 environment.systemPackages = [ pkgs.git ];
49 };
50 };
51
52 testScript = let
53 inherit (import ./ssh-keys.nix pkgs) snakeOilPrivateKey snakeOilPublicKey;
54 in ''
55 GIT_SSH_COMMAND = "ssh -i $HOME/.ssh/privk -o StrictHostKeyChecking=no"
56 REPO = "gitea@server:test/repo"
57 PRIVK = "${snakeOilPrivateKey}"
58
59 start_all()
60
61 client1.succeed("mkdir /tmp/repo")
62 client1.succeed("mkdir -p $HOME/.ssh")
63 client1.succeed(f"cat {PRIVK} > $HOME/.ssh/privk")
64 client1.succeed("chmod 0400 $HOME/.ssh/privk")
65 client1.succeed("git -C /tmp/repo init")
66 client1.succeed("echo hello world > /tmp/repo/testfile")
67 client1.succeed("git -C /tmp/repo add .")
68 client1.succeed("git config --global user.email test@localhost")
69 client1.succeed("git config --global user.name test")
70 client1.succeed("git -C /tmp/repo commit -m 'Initial import'")
71 client1.succeed(f"git -C /tmp/repo remote add origin {REPO}")
72
73 server.wait_for_unit("gitea.service")
74 server.wait_for_open_port(3000)
75 server.wait_for_open_port(22)
76 server.succeed("curl --fail http://localhost:3000/")
77
78 server.succeed(
79 "su -l gitea -c 'gpg --homedir /var/lib/gitea/data/home/.gnupg "
80 + "--import ${toString (pkgs.writeText "gitea.key" signingPrivateKey)}'"
81 )
82
83 assert "BEGIN PGP PUBLIC KEY BLOCK" in server.succeed("curl http://localhost:3000/api/v1/signing-key.gpg")
84
85 server.succeed(
86 "curl --fail http://localhost:3000/user/sign_up | grep 'Registration is disabled. "
87 + "Please contact your site administrator.'"
88 )
89 server.succeed(
90 "su -l gitea -c 'GITEA_WORK_DIR=/var/lib/gitea gitea admin user create "
91 + "--username test --password totallysafe --email test@localhost'"
92 )
93
94 api_token = server.succeed(
95 "curl --fail -X POST http://test:totallysafe@localhost:3000/api/v1/users/test/tokens "
96 + "-H 'Accept: application/json' -H 'Content-Type: application/json' -d "
97 + "'{\"name\":\"token\",\"scopes\":[\"all\"]}' | jq '.sha1' | xargs echo -n"
98 )
99
100 server.succeed(
101 "curl --fail -X POST http://localhost:3000/api/v1/user/repos "
102 + "-H 'Accept: application/json' -H 'Content-Type: application/json' "
103 + f"-H 'Authorization: token {api_token}'"
104 + ' -d \'{"auto_init":false, "description":"string", "license":"mit", "name":"repo", "private":false}\'''
105 )
106
107 server.succeed(
108 "curl --fail -X POST http://localhost:3000/api/v1/user/keys "
109 + "-H 'Accept: application/json' -H 'Content-Type: application/json' "
110 + f"-H 'Authorization: token {api_token}'"
111 + ' -d \'{"key":"${snakeOilPublicKey}","read_only":true,"title":"SSH"}\'''
112 )
113
114 client1.succeed(
115 f"GIT_SSH_COMMAND='{GIT_SSH_COMMAND}' git -C /tmp/repo push origin master"
116 )
117
118 client2.succeed("mkdir -p $HOME/.ssh")
119 client2.succeed(f"cat {PRIVK} > $HOME/.ssh/privk")
120 client2.succeed("chmod 0400 $HOME/.ssh/privk")
121 client2.succeed(f"GIT_SSH_COMMAND='{GIT_SSH_COMMAND}' git clone {REPO}")
122 client2.succeed('test "$(cat repo/testfile | xargs echo -n)" = "hello world"')
123
124 server.succeed(
125 'test "$(curl http://localhost:3000/api/v1/repos/test/repo/commits '
126 + '-H "Accept: application/json" | jq length)" = "1"'
127 )
128
129 client1.shutdown()
130 client2.shutdown()
131 server.shutdown()
132 '';
133 });
134in
135
136listToAttrs (map makeGiteaTest supportedDbTypes)