package main import ( "reflect" "testing" ) func TestConfig(t *testing.T) { t.Parallel() configData := []byte(` salt: 'xXxN4CLxXx' authors: read@stick.rocketnine.space: 'Read Access' check@stick.rocketnine.space: 'Check Access' write@stick.rocketnine.space: 'Write Access' notebooks: Test Notebook A: repo: /home/stick/repo/a serve: - read@stick.rocketnine.space: read - write@stick.rocketnine.space: write Test Notebook B: repo: /home/stick/repo/b serve: - check@stick.rocketnine.space: check - read@stick.rocketnine.space `) c, err := readConfigData(configData) if err != nil { t.Errorf("failed to load config data: %+v", err) } if c.Debug != false { t.Errorf("failed to read config option Debug: expected %t, got %t", false, c.Debug) } if c.Salt != "xXxN4CLxXx" { t.Errorf("failed to read config option Salt: expected %s, got %s", "xXxN4CLxXx", c.Salt) } expectedAuthors := []*authorConfig{ {Email: "read@stick.rocketnine.space", Name: "Read Access"}, {Email: "check@stick.rocketnine.space", Name: "Check Access"}, {Email: "write@stick.rocketnine.space", Name: "Write Access"}} for _, expectedAuthor := range expectedAuthors { found := 0 for _, author := range c.Authors { if author.Email == expectedAuthor.Email && author.Name == expectedAuthor.Name { found++ } } if found != 1 { t.Errorf("failed to read config option author: expected 1 %v, got %d", expectedAuthor, found) } } expectedNotebooks := []*notebookConfig{ {Label: "Test Notebook A", Repo: "/home/stick/repo/a", Serve: map[string]int{"read@stick.rocketnine.space": authorAccessRead, "write@stick.rocketnine.space": authorAccessWrite}}, {Label: "Test Notebook B", Repo: "/home/stick/repo/b", Serve: map[string]int{"check@stick.rocketnine.space": authorAccessCheck, "read@stick.rocketnine.space": authorAccessRead}}} for _, expectedNotebook := range expectedNotebooks { found := 0 for _, notebook := range c.Notebooks { if notebook.Label == expectedNotebook.Label && reflect.DeepEqual(notebook.Serve, expectedNotebook.Serve) && notebook.Repo == expectedNotebook.Repo { found++ } } if found != 1 { t.Errorf("failed to read config option notebook: expected 1 %v, got %d", expectedNotebook, found) } } }