2022-07-21 08:02:07 +00:00
package nginx
import (
"testing"
"npm/internal/entity/certificate"
"npm/internal/entity/host"
"github.com/stretchr/testify/assert"
)
func TestWriteTemplate ( t * testing . T ) {
template := `
{ { # if Host . IsDisabled } }
# Host is disabled
{ { else } }
server {
2022-11-08 00:03:45 +00:00
{ { # if Certificate . IsProvided } }
{ { # if Certificate . IsAcme } }
include { { ConfDir } } / npm / conf . d / acme - challenge . conf ;
include { { ConfDir } } / npm / conf . d / include / ssl - ciphers . conf ;
2022-07-21 08:02:07 +00:00
{ { / if } }
2022-11-08 00:03:45 +00:00
ssl_certificate { { Certificate . Folder } } / fullchain . pem ;
ssl_certificate_key { { Certificate . Folder } } / privkey . pem ;
2022-07-21 08:02:07 +00:00
{ { / if } }
}
{ { / if } }
`
type want struct {
output string
err error
}
tests := [ ] struct {
name string
data TemplateData
2022-11-08 00:03:45 +00:00
host host . Model
cert certificate . Model
2022-07-21 08:02:07 +00:00
want want
} {
{
name : "Basic Template enabled" ,
2022-11-08 00:03:45 +00:00
host : host . Model {
IsDisabled : false ,
} ,
cert : certificate . Model {
ID : 77 ,
Status : certificate . StatusProvided ,
Type : certificate . TypeHTTP ,
CertificateAuthorityID : 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 ,
} ,
} ,
{
name : "Basic Template custom ssl" ,
host : host . Model {
IsDisabled : false ,
} ,
cert : certificate . Model {
ID : 66 ,
Status : certificate . StatusProvided ,
Type : certificate . TypeCustom ,
2022-07-21 08:02:07 +00:00
} ,
want : want {
2022-11-08 00:03:45 +00:00
output : "\nserver {\n ssl_certificate /custom_ssl/npm-66/fullchain.pem;\n ssl_certificate_key /custom_ssl/npm-66/privkey.pem;\n}\n" ,
2022-07-21 08:02:07 +00:00
err : nil ,
} ,
} ,
{
name : "Basic Template disabled" ,
2022-11-08 00:03:45 +00:00
host : host . Model {
IsDisabled : true ,
2022-07-21 08:02:07 +00:00
} ,
2022-11-08 00:03:45 +00:00
cert : certificate . Model { } ,
2022-07-21 08:02:07 +00:00
want : want {
2022-11-08 00:03:45 +00:00
output : "\n # Host is disabled\n" ,
2022-07-21 08:02:07 +00:00
err : nil ,
} ,
} ,
}
for _ , test := range tests {
t . Run ( test . name , func ( st * testing . T ) {
2022-11-08 00:03:45 +00:00
templateData := TemplateData {
ConfDir : "/etc/nginx/conf.d" ,
DataDir : "/data" ,
Host : test . host . GetTemplate ( ) ,
Certificate : test . cert . GetTemplate ( ) ,
}
2023-01-06 01:42:02 +00:00
output , err := renderTemplate ( template , templateData )
2022-07-21 08:02:07 +00:00
assert . Equal ( t , test . want . err , err )
assert . Equal ( t , test . want . output , output )
} )
}
}