summaryrefslogtreecommitdiff
path: root/pkg/motd/motd_test.go
blob: 85a2f1c042ac285c3ec20a0d7a0d704db845248a (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package motd

import (
	"testing"
)

func TestGoodMotd(t *testing.T) {
	m, err := ParseFile(ExampleFile)
	if err != nil {
		t.Errorf("error parsing default file")
	}
	if m.Length() == 0 {
		t.Errorf("zero messages in file")
	}
	for _, msg := range m.Messages {
		if !msg.IsValid() {
			t.Errorf("invalid motd json at %s", ExampleFile)
		}
	}
}

const emptyDate = `
{
    "motd": [{
        "begin":    "",
        "end":      "",
        "type":     "daily",
        "platform": "all",
        "urgency":  "normal",
        "text": [
          { "lang": "en",
            "str": "test"
	  }]
    }]
}`

func TestEmptyDateFails(t *testing.T) {
	m, err := getFromJSON([]byte(emptyDate))
	if err != nil {
		t.Errorf("error parsing json")
	}
	if allValid(t, m) {
		t.Errorf("empty string should not be valid")
	}
}

const badEnd = `
{
    "motd": [{
	"begin":    "02 Jan 21 00:00 +0100",
	"end":      "01 Jan 21 00:00 +0100",
        "type":     "daily",
        "platform": "all",
        "urgency":  "normal",
        "text": [
          { "lang": "en",
            "str": "test"
	  }]
    }]
}`

func TestBadEnd(t *testing.T) {
	m, err := getFromJSON([]byte(badEnd))
	if err != nil {
		t.Errorf("error parsing json")
	}
	if allValid(t, m) {
		t.Errorf("begin > end must fail")
	}
}

func allValid(t *testing.T, m Messages) bool {
	if m.Length() == 0 {
		t.Errorf("expected at least one message")

	}
	for _, msg := range m.Messages {
		if !msg.IsValid() {
			return false
		}
	}
	return true
}