violet/domains/domains_test.go

48 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-06-03 19:33:06 +01:00
package domains
import (
"context"
"github.com/1f349/violet"
"github.com/1f349/violet/database"
2023-06-03 19:33:06 +01:00
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
"testing"
)
func TestDomainsNew(t *testing.T) {
db, err := violet.InitDB("file:TestDomainsNew?mode=memory&cache=shared")
2023-06-03 19:33:06 +01:00
assert.NoError(t, err)
domains := New(db)
err = db.AddDomain(context.Background(), database.AddDomainParams{Domain: "example.com", Active: true})
2023-06-03 19:33:06 +01:00
assert.NoError(t, err)
domains.Compile()
if _, ok := domains.m["example.com"]; ok {
assert.True(t, ok)
}
if _, ok := domains.m["www.example.com"]; !ok {
assert.False(t, ok)
}
}
func TestDomains_IsValid(t *testing.T) {
// open sqlite database
db, err := violet.InitDB("file:TestDomains_IsValid?mode=memory&cache=shared")
2023-06-03 19:33:06 +01:00
assert.NoError(t, err)
domains := New(db)
err = db.AddDomain(context.Background(), database.AddDomainParams{Domain: "example.com", Active: true})
2023-06-03 19:33:06 +01:00
assert.NoError(t, err)
domains.s.Lock()
assert.NoError(t, domains.internalCompile(domains.m))
domains.s.Unlock()
assert.True(t, domains.IsValid("example.com"))
assert.True(t, domains.IsValid("www.example.com"))
assert.False(t, domains.IsValid("notexample.com"))
assert.False(t, domains.IsValid("www.notexample.com"))
}