summaryrefslogtreecommitdiffstats
path: root/nixos/tests/dolibarr.nix
blob: 1680a88e40292c876255c50f58b5b1d46f4bc288 (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
{ ... }:
{
  name = "dolibarr";
  meta.maintainers = [ ];

  nodes = {
    nginx_mysql =
      { ... }:
      {
        services.dolibarr = {
          enable = true;
          domain = "localhost";
          nginx = {
            forceSSL = false;
            enableACME = false;
          };
          database.type = "mysql";
        };

        networking.firewall.allowedTCPPorts = [ 80 ];
      };
    h2o_postgresql =
      { ... }:
      {
        services.dolibarr = {
          enable = true;
          domain = "localhost";
          h2o = {
            acme.enable = false;
          };
          database.type = "postgresql";
        };

        networking.firewall.allowedTCPPorts = [ 80 ];
      };
  };

  testScript = # python
    ''
      from html.parser import HTMLParser
      start_all()

      csrf_token = None
      class TokenParser(HTMLParser):
        def handle_starttag(self, tag, attrs):
          attrs = dict(attrs) # attrs is an assoc list originally
          if tag == 'input' and attrs.get('name') == 'token':
              csrf_token = attrs.get('value')
              print(f'[+] Caught CSRF token: {csrf_token}')
        def handle_endtag(self, tag): pass
        def handle_data(self, data): pass

      # wait for app
      for machine in (nginx_mysql, h2o_postgresql):
        machine.wait_for_unit("phpfpm-dolibarr.service")

      # wait for web servers
      nginx_mysql.wait_for_unit("nginx.service")
      nginx_mysql.wait_for_open_port(80)
      h2o_postgresql.wait_for_unit("h2o.service")
      h2o_postgresql.wait_for_open_port(80)

      for machine in (nginx_mysql, h2o_postgresql):
        # Perform installation.
        machine.succeed('curl -fL -X POST http://localhost/install/check.php -F selectlang=auto')
        machine.succeed('curl -fL -X POST http://localhost/install/fileconf.php -F selectlang=auto')
        # First time is to write the configuration file correctly.
        machine.succeed('curl -fL -X POST http://localhost/install/step1.php -F "testpost=ok" -F "action=set" -F "selectlang=auto"')
        # Now, we have a proper conf.php in $stateDir.
        assert 'nixos' in machine.succeed("cat /var/lib/dolibarr/conf.php")
        machine.succeed('curl -fL -X POST http://localhost/install/step2.php --data "testpost=ok&action=set&dolibarr_main_db_character_set=utf8&dolibarr_main_db_collation=utf8_unicode_ci&selectlang=auto"')
        machine.succeed('curl -fL -X POST http://localhost/install/step4.php --data "testpost=ok&action=set&selectlang=auto"')
        machine.succeed('curl -fL -X POST http://localhost/install/step5.php --data "testpost=ok&action=set&login=root&pass=hunter2&pass_verif=hunter2&selectlang=auto"')
        # Now, we have installed the machine, let's verify we still have the right configuration.
        assert 'nixos' in machine.succeed("cat /var/lib/dolibarr/conf.php")
        # We do not want any redirect now as we have installed the machine.
        machine.succeed('curl -f -X GET http://localhost')
        # Test authentication to the webservice.
        parser = TokenParser()
        parser.feed(machine.succeed('curl -f -X GET http://localhost/index.php?mainmenu=login&username=root'))
        machine.succeed(f'curl -f -X POST http://localhost/index.php?mainmenu=login&token={csrf_token}&username=root&password=hunter2')
    '';
}