You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

149 lines
4.0 KiB

  1. package controller
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. "github.com/AFASystems/presence/internal/pkg/common/appcontext"
  10. "github.com/AFASystems/presence/internal/pkg/controller"
  11. "github.com/AFASystems/presence/internal/pkg/model"
  12. "github.com/gorilla/mux"
  13. "gorm.io/driver/sqlite"
  14. "gorm.io/gorm"
  15. )
  16. func setupTestDB(t *testing.T) *gorm.DB {
  17. db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
  18. if err != nil {
  19. t.Fatalf("Failed to open test DB: %v", err)
  20. }
  21. if err := db.AutoMigrate(&model.Gateway{}, &model.Zone{}, &model.Tracker{}, &model.TrackerZones{}, &model.Config{}, appcontext.Settings{}, &model.Tracks{}); err != nil {
  22. t.Fatalf("Failed to migrate: %v", err)
  23. }
  24. return db
  25. }
  26. func TestGatewayListController_Empty(t *testing.T) {
  27. db := setupTestDB(t)
  28. context := context.Background()
  29. handler := controller.GatewayListController(db, context)
  30. req := httptest.NewRequest(http.MethodGet, "/gateways", nil)
  31. rec := httptest.NewRecorder()
  32. handler.ServeHTTP(rec, req)
  33. if rec.Code != http.StatusOK {
  34. t.Errorf("Expected 200, got %d", rec.Code)
  35. }
  36. var gateways []model.Gateway
  37. if err := json.NewDecoder(rec.Body).Decode(&gateways); err != nil {
  38. t.Fatalf("Failed to decode: %v", err)
  39. }
  40. if len(gateways) != 0 {
  41. t.Errorf("Expected 0 gateways, got %d", len(gateways))
  42. }
  43. }
  44. func TestGatewayAddController(t *testing.T) {
  45. db := setupTestDB(t)
  46. context := context.Background()
  47. handler := controller.GatewayAddController(db, context)
  48. gateway := model.Gateway{
  49. ID: "gw-1",
  50. Name: "Gateway 1",
  51. MAC: "AA:BB:CC:DD:EE:FF",
  52. }
  53. body, _ := json.Marshal(gateway)
  54. req := httptest.NewRequest(http.MethodPost, "/gateways", bytes.NewReader(body))
  55. req.Header.Set("Content-Type", "application/json")
  56. rec := httptest.NewRecorder()
  57. handler.ServeHTTP(rec, req)
  58. if rec.Code != http.StatusOK {
  59. t.Errorf("Expected 200, got %d: %s", rec.Code, rec.Body.String())
  60. }
  61. if rec.Body.String() != "ok" {
  62. t.Errorf("Expected 'ok', got %s", rec.Body.String())
  63. }
  64. var list []model.Gateway
  65. db.Find(&list)
  66. if len(list) != 1 || list[0].Name != "Gateway 1" {
  67. t.Errorf("Expected 1 gateway in DB, got %+v", list)
  68. }
  69. }
  70. func TestGatewayDeleteController(t *testing.T) {
  71. db := setupTestDB(t)
  72. db.Create(&model.Gateway{ID: "gw-1", Name: "G1", MAC: "AA:BB:CC:DD:EE:FF"})
  73. context := context.Background()
  74. req := httptest.NewRequest(http.MethodDelete, "/gateways/gw-1", nil)
  75. req = mux.SetURLVars(req, map[string]string{"id": "gw-1"})
  76. rec := httptest.NewRecorder()
  77. controller.GatewayDeleteController(db, context).ServeHTTP(rec, req)
  78. if rec.Code != http.StatusOK {
  79. t.Errorf("Expected 200, got %d", rec.Code)
  80. }
  81. var count int64
  82. db.Model(&model.Gateway{}).Where("id = ?", "gw-1").Count(&count)
  83. if count != 0 {
  84. t.Error("Expected gateway to be deleted")
  85. }
  86. }
  87. func TestTrackerListController_Empty(t *testing.T) {
  88. db := setupTestDB(t)
  89. context := context.Background()
  90. handler := controller.TrackerList(db, context)
  91. req := httptest.NewRequest(http.MethodGet, "/trackers", nil)
  92. rec := httptest.NewRecorder()
  93. handler.ServeHTTP(rec, req)
  94. if rec.Code != http.StatusOK {
  95. t.Errorf("Expected 200, got %d", rec.Code)
  96. }
  97. var list []model.Tracker
  98. if err := json.NewDecoder(rec.Body).Decode(&list); err != nil {
  99. t.Fatalf("Failed to decode: %v", err)
  100. }
  101. if len(list) != 0 {
  102. t.Errorf("Expected 0 trackers, got %d", len(list))
  103. }
  104. }
  105. func TestZoneListController_Empty(t *testing.T) {
  106. db := setupTestDB(t)
  107. context := context.Background()
  108. handler := controller.ZoneListController(db, context)
  109. req := httptest.NewRequest(http.MethodGet, "/zones", nil)
  110. rec := httptest.NewRecorder()
  111. handler.ServeHTTP(rec, req)
  112. if rec.Code != http.StatusOK {
  113. t.Errorf("Expected 200, got %d", rec.Code)
  114. }
  115. }
  116. func TestSettingsListController(t *testing.T) {
  117. db := setupTestDB(t)
  118. context := context.Background()
  119. handler := controller.SettingsListController(db, context)
  120. req := httptest.NewRequest(http.MethodGet, "/settings", nil)
  121. rec := httptest.NewRecorder()
  122. handler.ServeHTTP(rec, req)
  123. if rec.Code != http.StatusOK {
  124. t.Errorf("Expected 200, got %d", rec.Code)
  125. }
  126. }