setupwizard_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package main
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "net/url"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. db "imuslab.com/arozos/mod/database"
  10. module "imuslab.com/arozos/mod/modules"
  11. "imuslab.com/arozos/mod/permission"
  12. )
  13. // newSetupWizardTestEnv points the globals the wizard uses at a fresh
  14. // installation: the administrator group boots into the wizard and the
  15. // Desktop, Drive, Setup Wizard and Photo modules are loaded.
  16. func newSetupWizardTestEnv(t *testing.T, withWizard bool) {
  17. t.Helper()
  18. oldDB, oldPerm, oldMod := sysdb, permissionHandler, moduleHandler
  19. t.Cleanup(func() { sysdb, permissionHandler, moduleHandler = oldDB, oldPerm, oldMod })
  20. database, err := db.NewDatabase(filepath.Join(t.TempDir(), "sys.db"), false)
  21. if err != nil {
  22. t.Fatalf("NewDatabase: %v", err)
  23. }
  24. t.Cleanup(database.Close)
  25. for _, table := range []string{"module", setupWizardTable} {
  26. if err := database.NewTable(table); err != nil {
  27. t.Fatalf("NewTable(%s): %v", table, err)
  28. }
  29. }
  30. sysdb = database
  31. ph, err := permission.NewPermissionHandler(database)
  32. if err != nil {
  33. t.Fatalf("NewPermissionHandler: %v", err)
  34. }
  35. ph.LoadPermissionGroupsFromDatabase()
  36. ph.NewPermissionGroup("family", false, 0, []string{}, "Desktop")
  37. permissionHandler = ph
  38. mh := module.NewModuleHandler(nil, t.TempDir())
  39. if err := mh.LoadDisabledModules(database); err != nil {
  40. t.Fatalf("LoadDisabledModules: %v", err)
  41. }
  42. mh.InterfaceModuleUsers = groupsBootingInto
  43. mh.RegisterModule(module.ModuleInfo{Name: "Desktop", Group: interfaceModGroup})
  44. mh.RegisterModuleFromAGI("web/Drive", `{"Name":"Drive","Group":"Interface Module","StartDir":"Drive/index.html"}`)
  45. mh.RegisterModuleFromAGI("web/Photo", `{"Name":"Photo","Group":"Media","StartDir":"Photo/index.html"}`)
  46. if withWizard {
  47. mh.RegisterModuleFromAGI("web/Setup Wizard", `{"Name":"Setup Wizard","Group":"Interface Module","StartDir":"Setup Wizard/index.html"}`)
  48. }
  49. moduleHandler = mh
  50. }
  51. func adminInterface(t *testing.T) string {
  52. t.Helper()
  53. return permissionHandler.GetPermissionGroupByName("administrator").DefaultInterfaceModule
  54. }
  55. func TestSetupInterfaces(t *testing.T) {
  56. newSetupWizardTestEnv(t, true)
  57. names := []string{}
  58. for _, iface := range setupInterfaces() {
  59. names = append(names, iface.Name)
  60. }
  61. if got := strings.Join(names, ","); got != "Desktop,Drive" {
  62. t.Errorf("setupInterfaces = %s, want Desktop,Drive (desktop first, no wizard, no apps)", got)
  63. }
  64. }
  65. func TestFinishSetupWizard(t *testing.T) {
  66. tests := []struct {
  67. name string
  68. withWizard bool
  69. target string
  70. wantErr string
  71. }{
  72. {name: "drive", withWizard: true, target: "Drive"},
  73. {name: "skip goes to the desktop", withWizard: true, target: "Desktop"},
  74. {name: "an app is not an interface", withWizard: true, target: "Photo", wantErr: "Not an interface module"},
  75. {name: "the wizard itself", withWizard: true, target: permission.SetupWizardModule, wantErr: "Not an interface module"},
  76. {name: "wizard folder removed", withWizard: false, target: "Desktop"},
  77. }
  78. for _, tt := range tests {
  79. t.Run(tt.name, func(t *testing.T) {
  80. newSetupWizardTestEnv(t, tt.withWizard)
  81. if !setupWizardActive() {
  82. t.Fatal("a fresh installation should boot into the wizard")
  83. }
  84. sysdb.Write(setupWizardTable, setupProgressKey, `{"step":3}`)
  85. err := finishSetupWizard(tt.target)
  86. if tt.wantErr != "" {
  87. if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
  88. t.Fatalf("finishSetupWizard(%q) error = %v, want %q", tt.target, err, tt.wantErr)
  89. }
  90. if adminInterface(t) != permission.SetupWizardModule {
  91. t.Error("a refused finish changed the admin interface")
  92. }
  93. return
  94. }
  95. if err != nil {
  96. t.Fatalf("finishSetupWizard(%q): %v", tt.target, err)
  97. }
  98. if got := adminInterface(t); got != tt.target {
  99. t.Errorf("admin interface = %q, want %q", got, tt.target)
  100. }
  101. if got := permissionHandler.GetPermissionGroupByName("family").DefaultInterfaceModule; got != "Desktop" {
  102. t.Errorf("other groups must keep their interface, family = %q", got)
  103. }
  104. if setupWizardActive() {
  105. t.Error("the wizard is still active after finishing")
  106. }
  107. if moduleHandler.GetModuleInfoByID(permission.SetupWizardModule) != nil {
  108. t.Error("the wizard module is still enabled")
  109. }
  110. if sysdb.KeyExists(setupWizardTable, setupProgressKey) {
  111. t.Error("the saved progress was not cleared")
  112. }
  113. })
  114. }
  115. }
  116. // An admin who switches the wizard on again and runs it gets the new choice
  117. func TestFinishSetupWizardRerun(t *testing.T) {
  118. newSetupWizardTestEnv(t, true)
  119. if err := finishSetupWizard("Drive"); err != nil {
  120. t.Fatalf("first finish: %v", err)
  121. }
  122. if err := moduleHandler.SetModuleEnabled(permission.SetupWizardModule, true); err != nil {
  123. t.Fatalf("enable the wizard again: %v", err)
  124. }
  125. if err := finishSetupWizard("Desktop"); err != nil {
  126. t.Fatalf("second finish: %v", err)
  127. }
  128. if got := adminInterface(t); got != "Desktop" {
  129. t.Errorf("admin interface = %q, want Desktop", got)
  130. }
  131. if !moduleHandler.IsModuleDisabled(permission.SetupWizardModule) {
  132. t.Error("the wizard should be disabled again")
  133. }
  134. }
  135. // The wizard cannot be switched off while a group still boots into it
  136. func TestSetupWizardCannotBeDisabledWhileInUse(t *testing.T) {
  137. newSetupWizardTestEnv(t, true)
  138. err := moduleHandler.SetModuleEnabled(permission.SetupWizardModule, false)
  139. if err == nil || !strings.Contains(err.Error(), "administrator") {
  140. t.Fatalf("SetModuleEnabled error = %v, want it to name the administrator group", err)
  141. }
  142. }
  143. func TestServeDisabledModuleFolder(t *testing.T) {
  144. newSetupWizardTestEnv(t, true)
  145. if err := finishSetupWizard("Desktop"); err != nil {
  146. t.Fatalf("finishSetupWizard: %v", err)
  147. }
  148. if err := moduleHandler.SetModuleEnabled("Photo", false); err != nil {
  149. t.Fatalf("disable Photo: %v", err)
  150. }
  151. tests := []struct {
  152. path string
  153. wantServed bool
  154. wantStatus int
  155. wantLoc string
  156. }{
  157. {path: "/Setup%20Wizard/index.html", wantServed: true, wantStatus: http.StatusTemporaryRedirect, wantLoc: "/"},
  158. {path: "/Photo/index.html", wantServed: true, wantStatus: http.StatusNotFound},
  159. {path: "/Photo/img/icon.png", wantServed: false}, //icons stay visible in the module manager
  160. {path: "/Setup%20Wizard/img/module_icon.svg", wantServed: false},
  161. {path: "/Drive/index.html", wantServed: false},
  162. {path: "/desktop.html", wantServed: false},
  163. {path: "/", wantServed: false},
  164. }
  165. for _, tt := range tests {
  166. t.Run(tt.path, func(t *testing.T) {
  167. u, _ := url.Parse(tt.path)
  168. req := httptest.NewRequest(http.MethodGet, u.String(), nil)
  169. rec := httptest.NewRecorder()
  170. served := serveDisabledModuleFolder(rec, req)
  171. if served != tt.wantServed {
  172. t.Fatalf("served = %v, want %v", served, tt.wantServed)
  173. }
  174. if !served {
  175. return
  176. }
  177. if rec.Code != tt.wantStatus {
  178. t.Errorf("status = %d, want %d", rec.Code, tt.wantStatus)
  179. }
  180. if tt.wantLoc != "" && rec.Header().Get("Location") != tt.wantLoc {
  181. t.Errorf("Location = %q, want %q", rec.Header().Get("Location"), tt.wantLoc)
  182. }
  183. })
  184. }
  185. }
  186. func TestHandleSetupProgress(t *testing.T) {
  187. newSetupWizardTestEnv(t, true)
  188. tests := []struct {
  189. name string
  190. form url.Values
  191. wantBody string
  192. wantSave string
  193. }{
  194. {name: "not json", form: url.Values{"progress": {"{step:"}}, wantBody: "Invalid progress"},
  195. {name: "too large", form: url.Values{"progress": {`"` + strings.Repeat("a", setupProgressLimit) + `"`}}, wantBody: "Invalid progress"},
  196. {name: "saved", form: url.Values{"progress": {`{"step":2,"interface":"Drive"}`}}, wantBody: "OK", wantSave: `{"step":2,"interface":"Drive"}`},
  197. {name: "cleared", form: url.Values{"clear": {"true"}}, wantBody: "OK"},
  198. }
  199. for _, tt := range tests {
  200. t.Run(tt.name, func(t *testing.T) {
  201. req := httptest.NewRequest(http.MethodPost, "/system/setup/progress", strings.NewReader(tt.form.Encode()))
  202. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  203. rec := httptest.NewRecorder()
  204. handleSetupProgress(rec, req)
  205. if !strings.Contains(rec.Body.String(), tt.wantBody) {
  206. t.Fatalf("body = %q, want %q", rec.Body.String(), tt.wantBody)
  207. }
  208. saved := ""
  209. sysdb.Read(setupWizardTable, setupProgressKey, &saved)
  210. if tt.wantSave != "" && saved != tt.wantSave {
  211. t.Errorf("saved = %q, want %q", saved, tt.wantSave)
  212. }
  213. if tt.name == "cleared" && sysdb.KeyExists(setupWizardTable, setupProgressKey) {
  214. t.Error("progress still saved after clear")
  215. }
  216. })
  217. }
  218. }