Skip to content

Commit

Permalink
TRIB-240: refactors connectAPI connect endpoint (#161)
Browse files Browse the repository at this point in the history
* TRIB-240: refactors connect code
- refactors code from multiple return statements to just two

* TRIB-240: adds connectAPI test for connect
- adds connect sad path test
  • Loading branch information
mrsbluerose authored Mar 4, 2024
1 parent 5789695 commit f5d977a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,8 @@ public ResponseEntity getQrCodeString(
@Connect
@PostMapping
public boolean connect(@RequestBody @Valid ConnectRequest connectRequest) {
if (connectService.validateQRCode(
connectRequest.qrcodePhrase, connectRequest.toBeConnectedWithUserId)) {
boolean isConnectionSaved =
connectService.saveConnectionDetails(
connectRequest.requestingUserId, connectRequest.toBeConnectedWithUserId);
if (isConnectionSaved) {
return true;
} else {
return false;
}
if (connectService.validateQRCode(connectRequest.qrcodePhrase, connectRequest.toBeConnectedWithUserId)) {
return connectService.saveConnectionDetails(connectRequest.requestingUserId, connectRequest.toBeConnectedWithUserId);
} else {
return false;
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/savvato/tribeapp/controllers/ConnectAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,33 @@ public void connectHappyPath() throws Exception {
assertEquals(toBeConnectedWithUserIdCaptor.getValue(), connectRequest.toBeConnectedWithUserId);
}

@Test
public void connectSadPath() throws Exception {
when(userPrincipalService.getUserPrincipalByEmail(Mockito.anyString()))
.thenReturn(new UserPrincipal(user));
String auth = AuthServiceImpl.generateAccessToken(user);
ConnectRequest connectRequest = new ConnectRequest();

connectRequest.requestingUserId = 1L;
connectRequest.toBeConnectedWithUserId = 2L;
connectRequest.qrcodePhrase = "ABCDEFGHIJKL";

when(connectService.validateQRCode(anyString(), anyLong())).thenReturn(true);
when(connectService.saveConnectionDetails(anyLong(), anyLong())).thenReturn(false);
this.mockMvc
.perform(
post("/api/connect")
.content(gson.toJson(connectRequest))
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + auth)
.characterEncoding("utf-8"))
.andExpect(status().isOk())
.andExpect(content().string("false"))
.andReturn();

}


@Test
public void connectWhenQrCodeInvalid() throws Exception {
when(userPrincipalService.getUserPrincipalByEmail(Mockito.anyString()))
Expand Down

0 comments on commit f5d977a

Please sign in to comment.