setting_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package main
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestBuildSettingTree(t *testing.T) {
  8. groups := []settingGroup{
  9. {Name: "Host Information", Group: "Info"},
  10. {Name: "Users & Groups", Group: "Users"},
  11. {Name: "Developer Options", Group: "Advance"},
  12. }
  13. mods := []settingModule{
  14. {Name: "Host Info", Group: "Info"},
  15. {Name: "Runtime", Group: "Info", RequireAdmin: true},
  16. {Name: "My Account", Group: "Users"},
  17. {Name: "System Log", Group: "Advance", RequireAdmin: true},
  18. {Name: "Orphan", Group: "Unknown"},
  19. }
  20. names := func(tree []settingGroupTree) map[string][]string {
  21. out := map[string][]string{}
  22. for _, g := range tree {
  23. for _, m := range g.Modules {
  24. out[g.Group] = append(out[g.Group], m.Name)
  25. }
  26. }
  27. return out
  28. }
  29. tests := []struct {
  30. name string
  31. isAdmin bool
  32. wantGroups []string
  33. wantPages map[string][]string
  34. }{
  35. {
  36. name: "admin sees every page in registration order",
  37. isAdmin: true,
  38. wantGroups: []string{"Info", "Users", "Advance"},
  39. wantPages: map[string][]string{
  40. "Info": {"Host Info", "Runtime"},
  41. "Users": {"My Account"},
  42. "Advance": {"System Log"},
  43. },
  44. },
  45. {
  46. name: "user loses admin pages and groups left empty",
  47. isAdmin: false,
  48. wantGroups: []string{"Info", "Users"},
  49. wantPages: map[string][]string{
  50. "Info": {"Host Info"},
  51. "Users": {"My Account"},
  52. },
  53. },
  54. }
  55. for _, tt := range tests {
  56. t.Run(tt.name, func(t *testing.T) {
  57. tree := buildSettingTree(groups, mods, tt.isAdmin)
  58. gotGroups := []string{}
  59. for _, g := range tree {
  60. gotGroups = append(gotGroups, g.Group)
  61. }
  62. if !reflect.DeepEqual(gotGroups, tt.wantGroups) {
  63. t.Errorf("groups = %v, want %v", gotGroups, tt.wantGroups)
  64. }
  65. if got := names(tree); !reflect.DeepEqual(got, tt.wantPages) {
  66. t.Errorf("pages = %v, want %v", got, tt.wantPages)
  67. }
  68. })
  69. }
  70. }
  71. func TestBuildSettingTreeEmptyAndJSON(t *testing.T) {
  72. tree := buildSettingTree(nil, nil, true)
  73. if tree == nil || len(tree) != 0 {
  74. t.Fatalf("expected an empty, non-nil tree, got %#v", tree)
  75. }
  76. tree = buildSettingTree(
  77. []settingGroup{{Name: "Host Information", Group: "Info", IconPath: "a.svg"}},
  78. []settingModule{{Name: "Host Info", Group: "Info", StartDir: "SystemAO/info/index.html"}},
  79. false,
  80. )
  81. js, err := json.Marshal(tree)
  82. if err != nil {
  83. t.Fatalf("marshal tree: %v", err)
  84. }
  85. var decoded []map[string]interface{}
  86. if err := json.Unmarshal(js, &decoded); err != nil {
  87. t.Fatalf("unmarshal tree: %v", err)
  88. }
  89. if len(decoded) != 1 {
  90. t.Fatalf("expected one group, got %d", len(decoded))
  91. }
  92. for _, key := range []string{"Name", "Group", "IconPath", "Modules"} {
  93. if _, ok := decoded[0][key]; !ok {
  94. t.Errorf("group JSON is missing %q: %s", key, js)
  95. }
  96. }
  97. }