Skip to content

Commit e5ade3b

Browse files
committed
Add more unit tests
1 parent 7f9a1f5 commit e5ade3b

File tree

11 files changed

+162
-83
lines changed

11 files changed

+162
-83
lines changed

backend/cmd/server/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var version string
2323
func main() {
2424
config.InitArgs(&version, &commit)
2525
config.Init(&version, &commit)
26+
config.CreateDataFolders()
27+
logger.Info("Build Version: %s (%s)", version, commit)
2628

2729
database.Migrate(func() {
2830
if err := jwt.LoadKeys(); err != nil {

backend/internal/config/config.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ func Init(version, commit *string) {
1919
}
2020

2121
initLogger()
22-
logger.Info("Build Version: %s (%s)", Version, Commit)
23-
createDataFolders()
2422
}
2523

2624
// InitIPRanges will initialise the config for the ipranges command

backend/internal/config/folders.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"os"
77
)
88

9-
// createDataFolders will recursively create these folders within the
9+
// CreateDataFolders will recursively create these folders within the
1010
// data folder defined in configuration.
11-
func createDataFolders() {
11+
func CreateDataFolders() {
1212
folders := []string{
1313
"access",
1414
"certificates",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package nginx
2+
3+
import (
4+
"npm/internal/entity/host"
5+
"npm/internal/model"
6+
"npm/internal/test"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestGetHostFilename(t *testing.T) {
13+
test.InitConfig(t)
14+
tests := []struct {
15+
name string
16+
host host.Model
17+
append string
18+
want string
19+
}{
20+
{
21+
"test1",
22+
host.Model{
23+
ModelBase: model.ModelBase{
24+
ID: 10,
25+
},
26+
},
27+
"",
28+
"/data/nginx/hosts/host_10.conf",
29+
},
30+
{
31+
"test2",
32+
host.Model{
33+
ModelBase: model.ModelBase{
34+
ID: 10,
35+
},
36+
},
37+
".deleted",
38+
"/data/nginx/hosts/host_10.conf.deleted",
39+
},
40+
}
41+
for _, tt := range tests {
42+
t.Run(tt.name, func(t *testing.T) {
43+
filename := getHostFilename(tt.host, tt.append)
44+
assert.Equal(t, tt.want, filename)
45+
})
46+
}
47+
}

backend/internal/nginx/template_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import (
66
"npm/internal/entity/certificate"
77
"npm/internal/entity/host"
88
"npm/internal/model"
9+
"npm/internal/test"
910
"npm/internal/types"
1011

1112
"github.com/stretchr/testify/assert"
1213
)
1314

14-
func TestWriteTemplate(t *testing.T) {
15+
func TestRenderTemplate(t *testing.T) {
16+
test.InitConfig(t)
17+
1518
template := `
1619
{{#if Host.IsDisabled}}
1720
# Host is disabled
@@ -55,8 +58,15 @@ server {
5558
CertificateAuthorityID: types.NullableDBUint{Uint: 99},
5659
},
5760
want: want{
58-
output: "\nserver {\n include /etc/nginx/conf.d/npm/conf.d/acme-challenge.conf;\n include /etc/nginx/conf.d/npm/conf.d/include/ssl-ciphers.conf;\n ssl_certificate /npm-77/fullchain.pem;\n ssl_certificate_key /npm-77/privkey.pem;\n}\n",
59-
err: nil,
61+
output: `
62+
server {
63+
include /etc/nginx/conf.d/npm/conf.d/acme-challenge.conf;
64+
include /etc/nginx/conf.d/npm/conf.d/include/ssl-ciphers.conf;
65+
ssl_certificate /data/.acme.sh/certs/npm-77/fullchain.pem;
66+
ssl_certificate_key /data/.acme.sh/certs/npm-77/privkey.pem;
67+
}
68+
`,
69+
err: nil,
6070
},
6171
},
6272
{
@@ -72,8 +82,13 @@ server {
7282
Type: certificate.TypeCustom,
7383
},
7484
want: want{
75-
output: "\nserver {\n ssl_certificate /custom_ssl/npm-66/fullchain.pem;\n ssl_certificate_key /custom_ssl/npm-66/privkey.pem;\n}\n",
76-
err: nil,
85+
output: `
86+
server {
87+
ssl_certificate /data/custom_ssl/npm-66/fullchain.pem;
88+
ssl_certificate_key /data/custom_ssl/npm-66/privkey.pem;
89+
}
90+
`,
91+
err: nil,
7792
},
7893
},
7994
{

backend/internal/nginx/templates.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ type TemplateData struct {
2929
}
3030

3131
func renderTemplate(template string, data TemplateData) (string, error) {
32-
logger.Debug("Rendering Template - Template: %s", template)
33-
logger.Debug("Rendering Template - Data: %+v", data)
3432
return raymond.Render(template, data)
3533
}
3634

backend/internal/serverevents/sse.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package serverevents
33
import (
44
"encoding/json"
55
"net/http"
6+
67
"npm/internal/logger"
78

89
"github.com/jc21/go-sse"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package serverevents
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestGet(t *testing.T) {
10+
s := Get()
11+
assert.NotEqual(t, nil, s)
12+
}
13+
14+
// This is just for code coverage more than anything
15+
func TestEverything(t *testing.T) {
16+
Get()
17+
SendMessage("test", "test", map[string]string{"user_id": "10"})
18+
SendChange("hosts")
19+
Shutdown()
20+
}

backend/internal/tags/filters_test.go

Lines changed: 14 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,86 +3,28 @@ package tags
33
import (
44
"npm/internal/util"
55
"testing"
6+
"time"
67

78
"github.com/stretchr/testify/assert"
89
)
910

1011
func TestGetFilterSchema(t *testing.T) {
11-
m := struct {
12-
ID int `filter:"id"`
13-
Name string `filter:"name"`
14-
}{
15-
ID: 1,
16-
Name: "John",
12+
type testDemo struct {
13+
ID uint `json:"id" gorm:"column:user_id" filter:"id,number"`
14+
Created time.Time `json:"created" gorm:"column:user_created_date" filter:"created,date"`
15+
Name string `json:"name" gorm:"column:user_name" filter:"name,string"`
16+
NickName string `json:"nickname" gorm:"column:user_nickname" filter:"nickname"`
17+
IsDisabled string `json:"is_disabled" gorm:"column:user_is_disabled" filter:"is_disabled,bool"`
18+
Permissions string `json:"permissions" gorm:"column:user_permissions" filter:"permissions,regex"`
19+
History string `json:"history" gorm:"column:user_history" filter:"history,regex,(id|name)"`
1720
}
1821

19-
filterSchema := util.PrettyPrintJSON(GetFilterSchema(m))
22+
m := testDemo{ID: 10, Name: "test"}
2023

21-
expectedSchema := `{
22-
"type": "array",
23-
"items": {
24-
"oneOf": [
25-
{
26-
"type": "object",
27-
"properties": {
28-
"field": {
29-
"type": "string",
30-
"pattern": "^id$"
31-
},
32-
"modifier": {
33-
"type": "string",
34-
"pattern": "^(equals|not|contains|starts|ends|in|notin)$"
35-
},
36-
"value": {
37-
"oneOf": [
38-
{
39-
"type": "string",
40-
"minLength": 1
41-
},
42-
{
43-
"type": "array",
44-
"items": {
45-
"type": "string",
46-
"minLength": 1
47-
}
48-
}
49-
]
50-
}
51-
}
52-
},
53-
{
54-
"type": "object",
55-
"properties": {
56-
"field": {
57-
"type": "string",
58-
"pattern": "^name$"
59-
},
60-
"modifier": {
61-
"type": "string",
62-
"pattern": "^(equals|not|contains|starts|ends|in|notin)$"
63-
},
64-
"value": {
65-
"oneOf": [
66-
{
67-
"type": "string",
68-
"minLength": 1
69-
},
70-
{
71-
"type": "array",
72-
"items": {
73-
"type": "string",
74-
"minLength": 1
75-
}
76-
}
77-
]
78-
}
79-
}
80-
}
81-
]
82-
}
83-
}`
84-
85-
assert.Equal(t, expectedSchema, filterSchema)
24+
filterSchema := GetFilterSchema(m)
25+
assert.Greater(t, len(filterSchema), 4000)
26+
// Trigger again for code coverage of cached item
27+
GetFilterSchema(m)
8628
}
8729

8830
func TestGetFilterTagSchema(t *testing.T) {

backend/internal/tags/reflect_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package tags
2+
3+
import (
4+
"npm/internal/model"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestGetName(t *testing.T) {
11+
type testDemo struct {
12+
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
13+
Type string `json:"type" gorm:"column:type" filter:"type,string"`
14+
}
15+
16+
m := testDemo{UserID: 10, Type: "test"}
17+
assert.Equal(t, "tags.testDemo", getName(m))
18+
}
19+
20+
func TestCache(t *testing.T) {
21+
name := "testdemo"
22+
// Should return error
23+
_, exists := getCache(name)
24+
assert.Equal(t, false, exists)
25+
26+
setCache(name, map[string]model.FilterMapValue{
27+
"test": {
28+
Field: "test",
29+
Type: "test",
30+
},
31+
})
32+
33+
// Should return value
34+
val, exists := getCache(name)
35+
assert.Equal(t, true, exists)
36+
assert.Equal(t, "test", val["test"].Field)
37+
assert.Equal(t, "test", val["test"].Type)
38+
}

0 commit comments

Comments
 (0)