aboutsummaryrefslogtreecommitdiff
path: root/pkg/config/app_test.go
blob: 5a6cf4114bdbe60b1098a09ff7d13c12cef4cf7a (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
package config

import (
	"os"
	"testing"
)

func TestAppvalidate(t *testing.T) {
	// WorkingDirectory
	t.Cleanup(func() { os.RemoveAll("no_permission/") })
	if err := os.Mkdir("no_permission/", 0000); err != nil {
		t.Fatal("Could not create test directory")
	}
	app := App{WorkingDirectory: "no_permission/cannot_work"}
	if err := app.validate(); err == nil {
		t.Fatal("no_permission/cannot_wor/k should not be a valid working directory")
	}
	app = App{WorkingDirectory: "no_permission/"}
	if err := app.validate(); err == nil {
		t.Fatal("no_permission/ should not be a valid working directory")
	}

	// MaxUsers
	t.Cleanup(func() { os.RemoveAll("var/") })
	app = App{
		WorkingDirectory: "var/",
		MaxUsers:         0,
	}
	if err := app.validate(); err == nil {
		t.Fatal("Negative MaxUsers should not be valid")
	}

	// AllowRegistration is just a bool, nothing to test

	// MaxNickLen
	t.Cleanup(func() { os.RemoveAll("var/") })
	app = App{
		WorkingDirectory: "var/",
		MaxUsers:         1,
		MaxNickLen:       0,
	}
	if err := app.validate(); err == nil {
		t.Fatal("Negative or zero MaxNickLen should not be valid.")
	}

	//MenuMaxIdleTime
	t.Cleanup(func() { os.RemoveAll("var/") })
	app = App{
		WorkingDirectory: "var/",
		MaxUsers:         512,
		MaxNickLen:       15,
		MenuMaxIdleTime:  0,
	}
	if err := app.validate(); err == nil {
		t.Fatal("Negative or zero MenuMaxIdleTime should not be valid.")
	}

	//PostLoginCommands are mostly tested from command_test.go
	app = App{
		WorkingDirectory: "var/",
		MaxUsers:         512,
		MaxNickLen:       15,
		MenuMaxIdleTime:  60,
	}
	if err := app.validate(); err != nil {
		t.Fatal("Empty PostLoginCommands list should be valid")
	}
	app = App{
		WorkingDirectory:  "var/",
		MaxUsers:          512,
		MaxNickLen:        15,
		MenuMaxIdleTime:   60,
		PostLoginCommands: []string{"invalid"},
	}
	if err := app.validate(); err == nil {
		t.Fatal("Invalid command in PostLoginCommands should not be valid")
	}

	// A valid App
	app = App{
		WorkingDirectory:  "var/",
		MaxUsers:          512,
		MaxNickLen:        15,
		MenuMaxIdleTime:   60,
		PostLoginCommands: []string{"wait"},
	}
	if err := app.validate(); err != nil {
		t.Fatal("A valid app should pass")
	}
}