chore: update user location structure and add validation tests
This commit is contained in:
@@ -37,6 +37,7 @@ export class RestAuth {
|
|||||||
password: string;
|
password: string;
|
||||||
timezone: string;
|
timezone: string;
|
||||||
location: {
|
location: {
|
||||||
|
name: string;
|
||||||
lat: number;
|
lat: number;
|
||||||
lon: number;
|
lon: number;
|
||||||
};
|
};
|
||||||
|
|||||||
+16
-4
@@ -63,7 +63,11 @@ describe("RestAuth", () => {
|
|||||||
username: "testuser",
|
username: "testuser",
|
||||||
password: "TestPassword123!",
|
password: "TestPassword123!",
|
||||||
timezone: "Europe/Berlin",
|
timezone: "Europe/Berlin",
|
||||||
location: "Berlin, Germany",
|
location: {
|
||||||
|
name: "Berlin",
|
||||||
|
lat: 52.52,
|
||||||
|
lon: 13.405,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
it("should register a new user successfully", async () => {
|
it("should register a new user successfully", async () => {
|
||||||
@@ -73,7 +77,11 @@ describe("RestAuth", () => {
|
|||||||
name: "testuser",
|
name: "testuser",
|
||||||
uuid: mockUUID,
|
uuid: mockUUID,
|
||||||
timezone: "Europe/Berlin",
|
timezone: "Europe/Berlin",
|
||||||
location: "Berlin, Germany",
|
location: {
|
||||||
|
name: "Berlin",
|
||||||
|
lat: 52.52,
|
||||||
|
lon: 13.405,
|
||||||
|
},
|
||||||
config: {isVisible: false, isAdmin: false, canBeModified: false},
|
config: {isVisible: false, isAdmin: false, canBeModified: false},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -91,9 +99,13 @@ describe("RestAuth", () => {
|
|||||||
name: "testuser",
|
name: "testuser",
|
||||||
password: hashedPassword,
|
password: hashedPassword,
|
||||||
uuid: mockUUID,
|
uuid: mockUUID,
|
||||||
config: {isVisible: false, isAdmin: false, canBeModified: false},
|
config: { isVisible: false, isAdmin: false, canBeModified: false },
|
||||||
timezone: "Europe/Berlin",
|
timezone: "Europe/Berlin",
|
||||||
location: "Berlin, Germany",
|
location: {
|
||||||
|
name: "Berlin",
|
||||||
|
lat: 52.52,
|
||||||
|
lon: 13.405,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+150
-1
@@ -58,6 +58,154 @@ describe("RestUser", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("PUT /me/location", () => {
|
||||||
|
const validLocationData = {
|
||||||
|
name: "Berlin",
|
||||||
|
lat: 52.52,
|
||||||
|
lon: 13.405
|
||||||
|
};
|
||||||
|
|
||||||
|
it("should update user location successfully", async () => {
|
||||||
|
const mockUser = {
|
||||||
|
id: "test-user-id",
|
||||||
|
name: "testuser",
|
||||||
|
uuid: "test-user-uuid",
|
||||||
|
location: validLocationData
|
||||||
|
};
|
||||||
|
|
||||||
|
mockedUserService.updateUserByUUID.mockResolvedValue(mockUser);
|
||||||
|
|
||||||
|
const response = await request(testEnv.app)
|
||||||
|
.put("/user/me/location")
|
||||||
|
.send(validLocationData)
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
expect(response.body.data).toEqual(mockUser);
|
||||||
|
expect(mockedUserService.updateUserByUUID).toHaveBeenCalledWith(
|
||||||
|
"test-user-uuid",
|
||||||
|
{ location: validLocationData }
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return bad request for missing name", async () => {
|
||||||
|
const invalidData = { lat: 52.52, lon: 13.405 };
|
||||||
|
|
||||||
|
const response = await request(testEnv.app)
|
||||||
|
.put("/user/me/location")
|
||||||
|
.send(invalidData)
|
||||||
|
.expect(400);
|
||||||
|
|
||||||
|
expect(response.body.data.details[0]).toContain("name");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return bad request for empty name", async () => {
|
||||||
|
const invalidData = { name: "", lat: 52.52, lon: 13.405 };
|
||||||
|
|
||||||
|
const response = await request(testEnv.app)
|
||||||
|
.put("/user/me/location")
|
||||||
|
.send(invalidData)
|
||||||
|
.expect(400);
|
||||||
|
|
||||||
|
expect(response.body.data.details[0]).toContain("name");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return bad request for missing lat", async () => {
|
||||||
|
const invalidData = { name: "Berlin", lon: 13.405 };
|
||||||
|
|
||||||
|
const response = await request(testEnv.app)
|
||||||
|
.put("/user/me/location")
|
||||||
|
.send(invalidData)
|
||||||
|
.expect(400);
|
||||||
|
|
||||||
|
expect(response.body.data.details[0]).toContain("lat");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return bad request for missing lon", async () => {
|
||||||
|
const invalidData = { name: "Berlin", lat: 52.52 };
|
||||||
|
|
||||||
|
const response = await request(testEnv.app)
|
||||||
|
.put("/user/me/location")
|
||||||
|
.send(invalidData)
|
||||||
|
.expect(400);
|
||||||
|
|
||||||
|
expect(response.body.data.details[0]).toContain("lon");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return bad request for non-number lat", async () => {
|
||||||
|
const invalidData = { name: "Berlin", lat: "not-a-number", lon: 13.405 };
|
||||||
|
|
||||||
|
const response = await request(testEnv.app)
|
||||||
|
.put("/user/me/location")
|
||||||
|
.send(invalidData)
|
||||||
|
.expect(400);
|
||||||
|
|
||||||
|
expect(response.body.data.details[0]).toContain("lat");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return bad request for non-number lon", async () => {
|
||||||
|
const invalidData = { name: "Berlin", lat: 52.52, lon: "not-a-number" };
|
||||||
|
|
||||||
|
const response = await request(testEnv.app)
|
||||||
|
.put("/user/me/location")
|
||||||
|
.send(invalidData)
|
||||||
|
.expect(400);
|
||||||
|
|
||||||
|
expect(response.body.data.details[0]).toContain("lon");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should accept negative coordinates", async () => {
|
||||||
|
const locationWithNegativeCoords = {
|
||||||
|
name: "Buenos Aires",
|
||||||
|
lat: -34.6037,
|
||||||
|
lon: -58.3816
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockUser = {
|
||||||
|
id: "test-user-id",
|
||||||
|
name: "testuser",
|
||||||
|
uuid: "test-user-uuid",
|
||||||
|
location: locationWithNegativeCoords
|
||||||
|
};
|
||||||
|
|
||||||
|
mockedUserService.updateUserByUUID.mockResolvedValue(mockUser);
|
||||||
|
|
||||||
|
const response = await request(testEnv.app)
|
||||||
|
.put("/user/me/location")
|
||||||
|
.send(locationWithNegativeCoords)
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
expect(response.body.data).toEqual(mockUser);
|
||||||
|
expect(mockedUserService.updateUserByUUID).toHaveBeenCalledWith(
|
||||||
|
"test-user-uuid",
|
||||||
|
{ location: locationWithNegativeCoords }
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should accept zero coordinates", async () => {
|
||||||
|
const locationWithZeroCoords = {
|
||||||
|
name: "Null Island",
|
||||||
|
lat: 0,
|
||||||
|
lon: 0
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockUser = {
|
||||||
|
id: "test-user-id",
|
||||||
|
name: "testuser",
|
||||||
|
uuid: "test-user-uuid",
|
||||||
|
location: locationWithZeroCoords
|
||||||
|
};
|
||||||
|
|
||||||
|
mockedUserService.updateUserByUUID.mockResolvedValue(mockUser);
|
||||||
|
|
||||||
|
const response = await request(testEnv.app)
|
||||||
|
.put("/user/me/location")
|
||||||
|
.send(locationWithZeroCoords)
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
expect(response.body.data).toEqual(mockUser);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("PUT /me/spotify", () => {
|
describe("PUT /me/spotify", () => {
|
||||||
const validSpotifyData = {
|
const validSpotifyData = {
|
||||||
accessToken: "access-token-123",
|
accessToken: "access-token-123",
|
||||||
@@ -510,4 +658,5 @@ describe("RestUser", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user