summaryrefslogtreecommitdiff
path: root/database/dns_test.go
blob: a8930212d566a42ac7470581970288eae7cf9da4 (plain) (blame)
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
package database

import (
	"database/sql"
	"path/filepath"
	"testing"
)

func dnsTestDB(t *testing.T) *sql.DB {
	t.Helper()
	db, err := sql.Open("sqlite3", filepath.Join(t.TempDir(), "dns.db"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { db.Close() })
	if _, err := Migrate(db); err != nil {
		t.Fatal(err)
	}
	return db
}

func TestRenameUserDNSDomain(t *testing.T) {
	db := dnsTestDB(t)
	if _, err := CreateUser(db, "u1", "tux"); err != nil {
		t.Fatal(err)
	}

	records := []*DNSRecord{
		{ID: "a", UserID: "u1", Name: "tux.penguins.lan.", Type: "CNAME", Content: "aabbccddeeff.tux.penguins.lan", TTL: 300},
		{ID: "b", UserID: "u1", Name: "www.tux.penguins.lan.", Type: "A", Content: "192.168.8.9", TTL: 60},
		// content that merely contains the domain mid-string must NOT be rewritten
		{ID: "c", UserID: "u1", Name: "note.tux.penguins.lan.", Type: "TXT", Content: "notmytux.penguins.lan", TTL: 60},
	}
	for _, r := range records {
		if _, err := SaveDNSRecord(db, r); err != nil {
			t.Fatal(err)
		}
	}

	if err := RenameUserDNSDomain(db, "u1", "tux.penguins.lan", "gentoo.penguins.lan"); err != nil {
		t.Fatal(err)
	}

	got, err := GetUserDNSRecords(db, "u1")
	if err != nil {
		t.Fatal(err)
	}
	byID := map[string]DNSRecord{}
	for _, r := range got {
		byID[r.ID] = r
	}

	if byID["a"].Name != "gentoo.penguins.lan." || byID["a"].Content != "aabbccddeeff.gentoo.penguins.lan" {
		t.Errorf("apex CNAME = %q -> %q, want gentoo + device target", byID["a"].Name, byID["a"].Content)
	}
	if byID["b"].Name != "www.gentoo.penguins.lan." || byID["b"].Content != "192.168.8.9" {
		t.Errorf("A record = %q -> %q, want www.gentoo + unchanged ip", byID["b"].Name, byID["b"].Content)
	}
	if byID["c"].Name != "note.gentoo.penguins.lan." || byID["c"].Content != "notmytux.penguins.lan" {
		t.Errorf("TXT = %q -> %q, want name moved but content untouched", byID["c"].Name, byID["c"].Content)
	}
}