Add more unit tests

This commit is contained in:
Jamie Curnow 2023-07-28 15:01:54 +10:00
parent 7f9a1f5a98
commit e5ade3b4ea
No known key found for this signature in database
GPG Key ID: FFBB624C43388E9E
11 changed files with 162 additions and 83 deletions

View File

@ -23,6 +23,8 @@ var version string
func main() {
config.InitArgs(&version, &commit)
config.Init(&version, &commit)
config.CreateDataFolders()
logger.Info("Build Version: %s (%s)", version, commit)
database.Migrate(func() {
if err := jwt.LoadKeys(); err != nil {

View File

@ -19,8 +19,6 @@ func Init(version, commit *string) {
}
initLogger()
logger.Info("Build Version: %s (%s)", Version, Commit)
createDataFolders()
}
// InitIPRanges will initialise the config for the ipranges command

View File

@ -6,9 +6,9 @@ import (
"os"
)
// createDataFolders will recursively create these folders within the
// CreateDataFolders will recursively create these folders within the
// data folder defined in configuration.
func createDataFolders() {
func CreateDataFolders() {
folders := []string{
"access",
"certificates",

View File

@ -0,0 +1,47 @@
package nginx
import (
"npm/internal/entity/host"
"npm/internal/model"
"npm/internal/test"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetHostFilename(t *testing.T) {
test.InitConfig(t)
tests := []struct {
name string
host host.Model
append string
want string
}{
{
"test1",
host.Model{
ModelBase: model.ModelBase{
ID: 10,
},
},
"",
"/data/nginx/hosts/host_10.conf",
},
{
"test2",
host.Model{
ModelBase: model.ModelBase{
ID: 10,
},
},
".deleted",
"/data/nginx/hosts/host_10.conf.deleted",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filename := getHostFilename(tt.host, tt.append)
assert.Equal(t, tt.want, filename)
})
}
}

View File

@ -6,12 +6,15 @@ import (
"npm/internal/entity/certificate"
"npm/internal/entity/host"
"npm/internal/model"
"npm/internal/test"
"npm/internal/types"
"github.com/stretchr/testify/assert"
)
func TestWriteTemplate(t *testing.T) {
func TestRenderTemplate(t *testing.T) {
test.InitConfig(t)
template := `
{{#if Host.IsDisabled}}
# Host is disabled
@ -55,8 +58,15 @@ server {
CertificateAuthorityID: types.NullableDBUint{Uint: 99},
},
want: want{
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",
err: nil,
output: `
server {
include /etc/nginx/conf.d/npm/conf.d/acme-challenge.conf;
include /etc/nginx/conf.d/npm/conf.d/include/ssl-ciphers.conf;
ssl_certificate /data/.acme.sh/certs/npm-77/fullchain.pem;
ssl_certificate_key /data/.acme.sh/certs/npm-77/privkey.pem;
}
`,
err: nil,
},
},
{
@ -72,8 +82,13 @@ server {
Type: certificate.TypeCustom,
},
want: want{
output: "\nserver {\n ssl_certificate /custom_ssl/npm-66/fullchain.pem;\n ssl_certificate_key /custom_ssl/npm-66/privkey.pem;\n}\n",
err: nil,
output: `
server {
ssl_certificate /data/custom_ssl/npm-66/fullchain.pem;
ssl_certificate_key /data/custom_ssl/npm-66/privkey.pem;
}
`,
err: nil,
},
},
{

View File

@ -29,8 +29,6 @@ type TemplateData struct {
}
func renderTemplate(template string, data TemplateData) (string, error) {
logger.Debug("Rendering Template - Template: %s", template)
logger.Debug("Rendering Template - Data: %+v", data)
return raymond.Render(template, data)
}

View File

@ -3,6 +3,7 @@ package serverevents
import (
"encoding/json"
"net/http"
"npm/internal/logger"
"github.com/jc21/go-sse"

View File

@ -0,0 +1,20 @@
package serverevents
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGet(t *testing.T) {
s := Get()
assert.NotEqual(t, nil, s)
}
// This is just for code coverage more than anything
func TestEverything(t *testing.T) {
Get()
SendMessage("test", "test", map[string]string{"user_id": "10"})
SendChange("hosts")
Shutdown()
}

View File

@ -3,86 +3,28 @@ package tags
import (
"npm/internal/util"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestGetFilterSchema(t *testing.T) {
m := struct {
ID int `filter:"id"`
Name string `filter:"name"`
}{
ID: 1,
Name: "John",
type testDemo struct {
ID uint `json:"id" gorm:"column:user_id" filter:"id,number"`
Created time.Time `json:"created" gorm:"column:user_created_date" filter:"created,date"`
Name string `json:"name" gorm:"column:user_name" filter:"name,string"`
NickName string `json:"nickname" gorm:"column:user_nickname" filter:"nickname"`
IsDisabled string `json:"is_disabled" gorm:"column:user_is_disabled" filter:"is_disabled,bool"`
Permissions string `json:"permissions" gorm:"column:user_permissions" filter:"permissions,regex"`
History string `json:"history" gorm:"column:user_history" filter:"history,regex,(id|name)"`
}
filterSchema := util.PrettyPrintJSON(GetFilterSchema(m))
m := testDemo{ID: 10, Name: "test"}
expectedSchema := `{
"type": "array",
"items": {
"oneOf": [
{
"type": "object",
"properties": {
"field": {
"type": "string",
"pattern": "^id$"
},
"modifier": {
"type": "string",
"pattern": "^(equals|not|contains|starts|ends|in|notin)$"
},
"value": {
"oneOf": [
{
"type": "string",
"minLength": 1
},
{
"type": "array",
"items": {
"type": "string",
"minLength": 1
}
}
]
}
}
},
{
"type": "object",
"properties": {
"field": {
"type": "string",
"pattern": "^name$"
},
"modifier": {
"type": "string",
"pattern": "^(equals|not|contains|starts|ends|in|notin)$"
},
"value": {
"oneOf": [
{
"type": "string",
"minLength": 1
},
{
"type": "array",
"items": {
"type": "string",
"minLength": 1
}
}
]
}
}
}
]
}
}`
assert.Equal(t, expectedSchema, filterSchema)
filterSchema := GetFilterSchema(m)
assert.Greater(t, len(filterSchema), 4000)
// Trigger again for code coverage of cached item
GetFilterSchema(m)
}
func TestGetFilterTagSchema(t *testing.T) {

View File

@ -0,0 +1,38 @@
package tags
import (
"npm/internal/model"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetName(t *testing.T) {
type testDemo struct {
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
Type string `json:"type" gorm:"column:type" filter:"type,string"`
}
m := testDemo{UserID: 10, Type: "test"}
assert.Equal(t, "tags.testDemo", getName(m))
}
func TestCache(t *testing.T) {
name := "testdemo"
// Should return error
_, exists := getCache(name)
assert.Equal(t, false, exists)
setCache(name, map[string]model.FilterMapValue{
"test": {
Field: "test",
Type: "test",
},
})
// Should return value
val, exists := getCache(name)
assert.Equal(t, true, exists)
assert.Equal(t, "test", val["test"].Field)
assert.Equal(t, "test", val["test"].Type)
}

View File

@ -1,7 +1,10 @@
package test
import (
"npm/internal/config"
"npm/internal/database"
"strings"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"gorm.io/driver/postgres"
@ -21,3 +24,18 @@ func Setup() (sqlmock.Sqlmock, error) {
database.SetDB(gormDB)
return mock, err
}
func InitConfig(t *testing.T, envs ...string) {
if len(envs) > 0 {
for _, env := range envs {
parts := strings.Split(env, "=")
if len(parts) == 2 {
t.Setenv(parts[0], parts[1])
}
}
}
version := "999.999.999"
commit := "abcd123"
config.Init(&version, &commit)
}