Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@RestController
Expand All @@ -21,6 +22,13 @@ class UsersController(private val userModel: UserModel) {
)
fun syncUser(@RequestBody request: UserSyncRequest): User = userModel.syncUser(request)

@GetMapping("/users/me", produces = [MediaType.APPLICATION_JSON_VALUE])
@Operation(
summary = "Get the authenticated user",
description = "Returns public info for the currently authenticated user, resolved from the session or API key.",
)
Comment thread
fhennig marked this conversation as resolved.
Outdated
fun getMe(@RequestParam userId: Long): PublicUser = userModel.getUser(userId)
Comment thread
fhennig marked this conversation as resolved.
Outdated

@GetMapping("/users/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
@Operation(
summary = "Get user by internal ID",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class UsersClient(private val mockMvc: MockMvc, private val objectMapper: Object
getUserRaw(id).andExpect(status().isOk),
)

fun getMeRaw(userId: Long): ResultActions = mockMvc.perform(get("/users/me").param("userId", userId.toString()))

fun getMe(userId: Long): PublicUser = deserializeJsonResponse(
getMeRaw(userId).andExpect(status().isOk),
)

/** Creates a user with a random GitHub ID and returns the internal Long user ID. */
fun createUser(): Long = syncUser(
UserSyncRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ class UsersControllerTest(@param:Autowired private val usersClient: UsersClient)
.andExpect(jsonPath("$.detail").value("User 999999999 not found"))
}

@Test
fun `WHEN getting me THEN returns own public user info`() {
val created = usersClient.syncUser(
Comment thread
fhennig marked this conversation as resolved.
Outdated
UserSyncRequest(githubId = UUID.randomUUID().toString(), name = "Eve", email = null),
)

val me = usersClient.getMe(created.id)

assertThat(me.id, equalTo(created.id))
assertThat(me.name, equalTo("Eve"))
}

@Test
fun `WHEN getting me with nonexistent user ID THEN returns 404`() {
usersClient.getMeRaw(999999999L)
Comment thread
fhennig marked this conversation as resolved.
Outdated
.andExpect(status().isNotFound)
.andExpect(jsonPath("$.detail").value("User 999999999 not found"))
}

@Test
fun `WHEN syncing two different github IDs THEN they get different internal IDs`() {
val first = usersClient.syncUser(
Expand Down
4 changes: 4 additions & 0 deletions website/src/backendApi/backendService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ export class BackendService extends ApiService {
});
}

public async getMe() {
return this.get({ url: '/users/me', schema: publicUserSchema });
}
Comment thread
fhennig marked this conversation as resolved.
Outdated
Comment thread
fhennig marked this conversation as resolved.
Outdated

public async getUser({ id }: { id: number }) {
return this.get({ url: `/users/${id}`, schema: publicUserSchema });
}
Expand Down
3 changes: 3 additions & 0 deletions website/src/pages/api/users/me.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { proxyToBackend } from '../../../backendApi/backendProxy.ts';

export const GET = proxyToBackend;
Loading