summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--branding/motd-cli/main.go44
-rw-r--r--branding/motd-cli/motd-example.json4
-rw-r--r--branding/motd-cli/motd_test.go83
3 files changed, 120 insertions, 11 deletions
diff --git a/branding/motd-cli/main.go b/branding/motd-cli/main.go
index 6828cc9..0ac1316 100644
--- a/branding/motd-cli/main.go
+++ b/branding/motd-cli/main.go
@@ -6,16 +6,18 @@ import (
"fmt"
"io"
"io/ioutil"
+ "log"
"net/http"
"os"
+ "time"
)
-const defaultFile = "motd-example.json"
+/* TODO move structs to pkg/config/motd module, import from there */
+const defaultFile = "motd-example.json"
const OK = "✓"
const WRONG = "☓"
-
-/* TODO move structs to pkg/config/motd module, import from there */
+const TimeString = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
type Messages struct {
Messages []Message `json:"motd"`
@@ -42,12 +44,29 @@ func (m *Message) IsValid() bool {
}
func (m *Message) IsValidBegin() bool {
- // FIXME check that begin is before 1y for instance
+ _, err := time.Parse(TimeString, m.Begin)
+ if err != nil {
+ log.Println(err)
+ return false
+ }
return true
}
func (m *Message) IsValidEnd() bool {
- // FIXME check end is within next year/months
+ endTime, err := time.Parse(TimeString, m.End)
+ if err != nil {
+ log.Println(err)
+ return false
+ }
+ beginTime, err := time.Parse(TimeString, m.Begin)
+ if err != nil {
+ log.Println(err)
+ return false
+ }
+ if !beginTime.Before(endTime) {
+ log.Println("begin ts should be before end")
+ return false
+ }
return true
}
@@ -104,7 +123,10 @@ func main() {
}
fmt.Println("file:", f)
}
- m := parseFile(f)
+ m, err := parseFile(f)
+ if err != nil {
+ panic(err)
+ }
fmt.Printf("count: %v\n", m.Length())
fmt.Println()
for i, msg := range m.Messages {
@@ -137,7 +159,7 @@ func downloadToTempFile(url string) string {
return out.Name()
}
-func parseFile(f string) Messages {
+func parseFile(f string) (Messages, error) {
jsonFile, err := os.Open(f)
if err != nil {
panic(err)
@@ -147,9 +169,13 @@ func parseFile(f string) Messages {
if err != nil {
panic(err)
}
+ return parseJsonStr(byteVal)
+}
+
+func parseJsonStr(b []byte) (Messages, error) {
var m Messages
- json.Unmarshal(byteVal, &m)
- return m
+ json.Unmarshal(b, &m)
+ return m, nil
}
func mark(val bool) string {
diff --git a/branding/motd-cli/motd-example.json b/branding/motd-cli/motd-example.json
index 9ebb5f5..f33c2b9 100644
--- a/branding/motd-cli/motd-example.json
+++ b/branding/motd-cli/motd-example.json
@@ -1,7 +1,7 @@
{
"motd": [{
- "begin": "Jan 1 2021 00:00:00",
- "end": "Dec 31 2021 23:59:00",
+ "begin": "02 Jan 21 15:04 -0700",
+ "end": "31 Jan 22 15:04 -0700",
"type": "daily",
"platform": "all",
"urgency": "normal",
diff --git a/branding/motd-cli/motd_test.go b/branding/motd-cli/motd_test.go
new file mode 100644
index 0000000..ed11661
--- /dev/null
+++ b/branding/motd-cli/motd_test.go
@@ -0,0 +1,83 @@
+package main
+
+import (
+ "testing"
+)
+
+func TestGoodMotd(t *testing.T) {
+ m, err := parseFile(defaultFile)
+ 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", defaultFile)
+ }
+ }
+}
+
+const emptyDate = `
+{
+ "motd": [{
+ "begin": "",
+ "end": "",
+ "type": "daily",
+ "platform": "all",
+ "urgency": "normal",
+ "text": [
+ { "lang": "en",
+ "str": "test"
+ }]
+ }]
+}`
+
+func TestEmptyDateFails(t *testing.T) {
+ m, err := parseJsonStr([]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 := parseJsonStr([]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
+}