fix(tests): Fix t.Run block variable scope issues
This commit is contained in:
@@ -65,7 +65,7 @@ func TestUpdateUserMaxDevices(t *testing.T) {
|
|||||||
}
|
}
|
||||||
jsonData, _ := json.Marshal(payload)
|
jsonData, _ := json.Marshal(payload)
|
||||||
|
|
||||||
req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData))
|
req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("Authorization", "Bearer "+adminToken)
|
req.Header.Set("Authorization", "Bearer "+adminToken)
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ func TestUpdateUserMaxDevicesValidation(t *testing.T) {
|
|||||||
}
|
}
|
||||||
jsonData, _ := json.Marshal(payload)
|
jsonData, _ := json.Marshal(payload)
|
||||||
|
|
||||||
req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData))
|
req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("Authorization", "Bearer "+adminToken)
|
req.Header.Set("Authorization", "Bearer "+adminToken)
|
||||||
|
|
||||||
@@ -164,7 +164,7 @@ func TestUpdateUserMaxDevicesAuth(t *testing.T) {
|
|||||||
}
|
}
|
||||||
jsonData, _ := json.Marshal(payload)
|
jsonData, _ := json.Marshal(payload)
|
||||||
|
|
||||||
req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData))
|
req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
@@ -187,7 +187,7 @@ func TestUpdateUserMaxDevicesAuth(t *testing.T) {
|
|||||||
}
|
}
|
||||||
jsonData, _ := json.Marshal(payload)
|
jsonData, _ := json.Marshal(payload)
|
||||||
|
|
||||||
req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData))
|
req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users/"+userID.String()+"/max-devices", bytes.NewBuffer(jsonData))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("Authorization", "Bearer "+regularToken)
|
req.Header.Set("Authorization", "Bearer "+regularToken)
|
||||||
|
|
||||||
@@ -218,7 +218,7 @@ func TestUpdateUserMaxDevicesNonExistentUser(t *testing.T) {
|
|||||||
}
|
}
|
||||||
jsonData, _ := json.Marshal(payload)
|
jsonData, _ := json.Marshal(payload)
|
||||||
|
|
||||||
req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users/"+nonExistentUserID.String()+"/max-devices", bytes.NewBuffer(jsonData))
|
req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users/"+nonExistentUserID.String()+"/max-devices", bytes.NewBuffer(jsonData))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("Authorization", "Bearer "+adminToken)
|
req.Header.Set("Authorization", "Bearer "+adminToken)
|
||||||
|
|
||||||
@@ -247,7 +247,7 @@ func TestUpdateUserMaxDevicesMissingUserID(t *testing.T) {
|
|||||||
jsonData, _ := json.Marshal(payload)
|
jsonData, _ := json.Marshal(payload)
|
||||||
|
|
||||||
// Missing user ID in URL
|
// Missing user ID in URL
|
||||||
req, _ := http.NewRequest("PUT", ts.URL+"/api/auth/users//max-devices", bytes.NewBuffer(jsonData))
|
req, _ := http.NewRequest("PUT", setup.Server.URL+"/api/auth/users//max-devices", bytes.NewBuffer(jsonData))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("Authorization", "Bearer "+adminToken)
|
req.Header.Set("Authorization", "Bearer "+adminToken)
|
||||||
|
|
||||||
@@ -269,7 +269,7 @@ func TestListUsersIncludesMaxDevices(t *testing.T) {
|
|||||||
adminUserID := getTestUserID(t, setup.DB)
|
adminUserID := getTestUserID(t, setup.DB)
|
||||||
adminToken = getAdminToken(t, setup.Server, adminUserID)
|
adminToken = getAdminToken(t, setup.Server, adminUserID)
|
||||||
|
|
||||||
req, _ := http.NewRequest("GET", ts.URL+"/api/auth/users", nil)
|
req, _ := http.NewRequest("GET", setup.Server.URL+"/api/auth/users", nil)
|
||||||
req.Header.Set("Authorization", "Bearer "+adminToken)
|
req.Header.Set("Authorization", "Bearer "+adminToken)
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
@@ -304,7 +304,7 @@ func createAdminUser(t *testing.T, ts *httptest.Server, token string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
jsonData, _ := json.Marshal(createUserPayload)
|
jsonData, _ := json.Marshal(createUserPayload)
|
||||||
req, _ := http.NewRequest("POST", ts.URL+"/api/auth/register", bytes.NewBuffer(jsonData))
|
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/auth/register", bytes.NewBuffer(jsonData))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("Authorization", "Bearer "+token)
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
|
|
||||||
@@ -324,7 +324,7 @@ func createTestUserForMaxDevices(t *testing.T, ts *httptest.Server, adminToken s
|
|||||||
}
|
}
|
||||||
|
|
||||||
jsonData, _ := json.Marshal(createUserPayload)
|
jsonData, _ := json.Marshal(createUserPayload)
|
||||||
req, _ := http.NewRequest("POST", ts.URL+"/api/auth/register", bytes.NewBuffer(jsonData))
|
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/auth/register", bytes.NewBuffer(jsonData))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("Authorization", "Bearer "+adminToken)
|
req.Header.Set("Authorization", "Bearer "+adminToken)
|
||||||
|
|
||||||
@@ -342,7 +342,7 @@ func createTestUserForMaxDevices(t *testing.T, ts *httptest.Server, adminToken s
|
|||||||
"password": "Test@Pass123!",
|
"password": "Test@Pass123!",
|
||||||
}
|
}
|
||||||
loginData, _ := json.Marshal(loginPayload)
|
loginData, _ := json.Marshal(loginPayload)
|
||||||
loginReq, _ := http.NewRequest("POST", ts.URL+"/api/auth/login", bytes.NewBuffer(loginData))
|
loginReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/auth/login", bytes.NewBuffer(loginData))
|
||||||
loginReq.Header.Set("Content-Type", "application/json")
|
loginReq.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
loginResp, err := client.Do(loginReq)
|
loginResp, err := client.Do(loginReq)
|
||||||
@@ -420,7 +420,7 @@ func getAdminToken(t *testing.T, ts *httptest.Server, userID uuid.UUID) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
jsonData, _ := json.Marshal(loginPayload)
|
jsonData, _ := json.Marshal(loginPayload)
|
||||||
req, _ := http.NewRequest("POST", ts.URL+"/api/auth/login", bytes.NewBuffer(jsonData))
|
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/auth/login", bytes.NewBuffer(jsonData))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
@@ -448,7 +448,7 @@ func loginTestUserByCredentials(t *testing.T, ts *httptest.Server, email, passwo
|
|||||||
}
|
}
|
||||||
|
|
||||||
jsonData, _ := json.Marshal(loginPayload)
|
jsonData, _ := json.Marshal(loginPayload)
|
||||||
req, _ := http.NewRequest("POST", ts.URL+"/api/auth/login", bytes.NewBuffer(jsonData))
|
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/auth/login", bytes.NewBuffer(jsonData))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func createTestLibrary(t *testing.T, ts *httptest.Server, token, name string) st
|
|||||||
}
|
}
|
||||||
|
|
||||||
body, _ := json.Marshal(payload)
|
body, _ := json.Marshal(payload)
|
||||||
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/libraries", bytes.NewBuffer(body))
|
req, _ := http.NewRequest("POST", ts.URL+"/api/libraries", bytes.NewBuffer(body))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("Authorization", "Bearer "+token)
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
|
|
||||||
|
|||||||
@@ -19,12 +19,12 @@ import (
|
|||||||
func TestListAllQueueItems_Admin(t *testing.T) {
|
func TestListAllQueueItems_Admin(t *testing.T) {
|
||||||
setup := setupTestServer(t)
|
setup := setupTestServer(t)
|
||||||
|
|
||||||
token := loginAdminUser(t, ts, db)
|
token := loginAdminUser(t, setup.Server, db)
|
||||||
|
|
||||||
req := httptest.NewRequest("GET", "/api/queue/items", nil)
|
req := httptest.NewRequest("GET", "/api/queue/items", nil)
|
||||||
req.Header.Set("Authorization", "Bearer "+token)
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
ts.Config.Handler.ServeHTTP(rec, req)
|
setup.Server.Config.Handler.ServeHTTP(rec, req)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusOK, rec.Code, "Should list all queue items")
|
assert.Equal(t, http.StatusOK, rec.Code, "Should list all queue items")
|
||||||
|
|
||||||
@@ -60,7 +60,7 @@ func TestGetDeviceQueueStats(t *testing.T) {
|
|||||||
req := httptest.NewRequest("GET", fmt.Sprintf("/api/queue/devices/%s/stats", deviceID.String()), nil)
|
req := httptest.NewRequest("GET", fmt.Sprintf("/api/queue/devices/%s/stats", deviceID.String()), nil)
|
||||||
req.Header.Set("Authorization", "Bearer "+token)
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
ts.Config.Handler.ServeHTTP(rec, req)
|
setup.Server.Config.Handler.ServeHTTP(rec, req)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusOK, rec.Code, "Should get device queue stats")
|
assert.Equal(t, http.StatusOK, rec.Code, "Should get device queue stats")
|
||||||
|
|
||||||
@@ -96,7 +96,7 @@ func TestListDeviceQueueItems(t *testing.T) {
|
|||||||
req := httptest.NewRequest("GET", fmt.Sprintf("/api/queue/devices/%s/items", deviceID.String()), nil)
|
req := httptest.NewRequest("GET", fmt.Sprintf("/api/queue/devices/%s/items", deviceID.String()), nil)
|
||||||
req.Header.Set("Authorization", "Bearer "+token)
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
ts.Config.Handler.ServeHTTP(rec, req)
|
setup.Server.Config.Handler.ServeHTTP(rec, req)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusOK, rec.Code, "Should list device queue items")
|
assert.Equal(t, http.StatusOK, rec.Code, "Should list device queue items")
|
||||||
|
|
||||||
@@ -115,7 +115,7 @@ func TestRetryQueueItem(t *testing.T) {
|
|||||||
req := httptest.NewRequest("POST", fmt.Sprintf("/api/queue/items/%s/retry", itemID), nil)
|
req := httptest.NewRequest("POST", fmt.Sprintf("/api/queue/items/%s/retry", itemID), nil)
|
||||||
req.Header.Set("Authorization", "Bearer "+token)
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
ts.Config.Handler.ServeHTTP(rec, req)
|
setup.Server.Config.Handler.ServeHTTP(rec, req)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusOK, rec.Code, "Should retry queue item")
|
assert.Equal(t, http.StatusOK, rec.Code, "Should retry queue item")
|
||||||
}
|
}
|
||||||
@@ -130,7 +130,7 @@ func TestDeleteQueueItem(t *testing.T) {
|
|||||||
req := httptest.NewRequest("DELETE", fmt.Sprintf("/api/queue/items/%s", itemID), nil)
|
req := httptest.NewRequest("DELETE", fmt.Sprintf("/api/queue/items/%s", itemID), nil)
|
||||||
req.Header.Set("Authorization", "Bearer "+token)
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
ts.Config.Handler.ServeHTTP(rec, req)
|
setup.Server.Config.Handler.ServeHTTP(rec, req)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusOK, rec.Code, "Should delete queue item")
|
assert.Equal(t, http.StatusOK, rec.Code, "Should delete queue item")
|
||||||
}
|
}
|
||||||
@@ -162,7 +162,7 @@ func TestClearDeviceQueue(t *testing.T) {
|
|||||||
req := httptest.NewRequest("DELETE", fmt.Sprintf("/api/queue/devices/%s/clear", deviceID.String()), nil)
|
req := httptest.NewRequest("DELETE", fmt.Sprintf("/api/queue/devices/%s/clear", deviceID.String()), nil)
|
||||||
req.Header.Set("Authorization", "Bearer "+token)
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
ts.Config.Handler.ServeHTTP(rec, req)
|
setup.Server.Config.Handler.ServeHTTP(rec, req)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusOK, rec.Code, "Should clear device queue")
|
assert.Equal(t, http.StatusOK, rec.Code, "Should clear device queue")
|
||||||
}
|
}
|
||||||
@@ -189,7 +189,7 @@ func TestQueueEndpoints_Unauthorized(t *testing.T) {
|
|||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
req := httptest.NewRequest(tt.method, tt.endpoint, bytes.NewReader(tt.body))
|
req := httptest.NewRequest(tt.method, tt.endpoint, bytes.NewReader(tt.body))
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
ts.Config.Handler.ServeHTTP(rec, req)
|
setup.Server.Config.Handler.ServeHTTP(rec, req)
|
||||||
|
|
||||||
assert.Equal(t, tt.expectedStatus, rec.Code, "Should require authentication")
|
assert.Equal(t, tt.expectedStatus, rec.Code, "Should require authentication")
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user