Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
mnhock authored and mnhock committed Sep 28, 2023
1 parent 3b0be2d commit 9538b2f
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Tired of scattered spreadsheets and scattered information? Our user-friendly int

<br/>

<div style="display: flex; justify-content: center">
<div style="display: flex; justify-content: center;">
<img src="https://github.com/enofex/naikan/blob/main/docs/assets/screenshots/projects-overview.png" width="49%">
<img src="https://github.com/enofex/naikan/blob/main/docs/assets/screenshots/project-detail-deployments.png" width="49%">
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ See all projects at a glance, filter, sort and search them.

### Projects insights

See all projects statisics at a glance, filter, sort and search them.
See all projects statistics at a glance, filter, sort and search them.

<img src="../assets/screenshots/projects-insights.png" alt="Projects insights" width="1024"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,7 @@ private void commits(Workbook workbook) {
put("File changed", commit -> String.valueOf(commit.changes().files().changed()));
}};

writeRows(sheet, columns,
this.bom.repository() != null ? this.bom.repository().commits().all() : List.of());
writeRows(sheet, columns, this.bom.repository().commits().all());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import static com.enofex.naikan.test.model.Boms.validBom0asInputStream;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.handler;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.enofex.naikan.administration.user.User;
import com.enofex.naikan.model.Bom;
import com.enofex.naikan.model.deserializer.DeserializerFactory;
import com.enofex.naikan.test.IntegrationTest;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
Expand All @@ -24,6 +27,8 @@ class ProjectControllerIT {
private MockMvc mvc;
@Autowired
private MongoTemplate template;
@Autowired
private ObjectMapper mapper;

@Test
void shouldFindAll() throws Exception {
Expand Down Expand Up @@ -254,4 +259,32 @@ void shouldFindRepositoryBranchesById() throws Exception {
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}

@Test
void shouldNotUpdateUserFavoritesWhenUserNotFound() throws Exception {
this.mvc.perform(
patch("/api/projects/favorites")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(this.mapper.writeValueAsString(new String[]{"not_important"})))
.andExpect(handler().methodName("updateUserFavorites"))
.andExpect(status().isNotFound());
}

@Test
void shouldUpdateUserFavorites() throws Exception {
this.template.save(new User("user"), "users");

Bom savedBom = this.template.save(
DeserializerFactory.newJsonDeserializer().of(validBom0asInputStream()),
"projects");

this.mvc.perform(
patch("/api/projects/favorites")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(this.mapper.writeValueAsString(new String[]{savedBom.id()})))
.andExpect(handler().methodName("updateUserFavorites"))
.andExpect(status().isNoContent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.enofex.naikan.security;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.security.core.AuthenticationException;

class HttpStatusReturningAuthenticationFailureHandlerTest {

private HttpStatusReturningAuthenticationFailureHandler failureHandler;
private ObjectMapper objectMapper;

@BeforeEach
void setUp() {
this.failureHandler = new HttpStatusReturningAuthenticationFailureHandler();
this.objectMapper = new ObjectMapper();
}

@Test
void shouldReturnCorrectValuesAuthenticationFailure() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
AuthenticationException exception = mock(AuthenticationException.class);
PrintWriter writer = mock(PrintWriter.class);

when(response.getWriter()).thenReturn(writer);

this.failureHandler.onAuthenticationFailure(request, response, exception);

verify(response).setCharacterEncoding(StandardCharsets.UTF_8.name());
verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
verify(writer).write(objectMapper.writeValueAsString(Mockito.any()));
verify(writer).flush();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.enofex.naikan.security;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.enofex.naikan.administration.user.AdministrationUserService;
import com.enofex.naikan.administration.user.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.security.core.Authentication;

class HttpStatusReturningAuthenticationSuccessHandlerTest {

private HttpStatusReturningAuthenticationSuccessHandler successHandler;
private AdministrationUserService userService;

@BeforeEach
void setUp() {
this.userService = mock(AdministrationUserService.class);
this.successHandler = new HttpStatusReturningAuthenticationSuccessHandler(this.userService);
}

@Test
void shouldAuthenticateFirstUserAndShouldBeAdmin() {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
Authentication authentication = mock(Authentication.class);

when(this.userService.count()).thenReturn(0L);
when(authentication.getName()).thenReturn("firstUser");

this.successHandler.onAuthenticationSuccess(request, response, authentication);

verify(this.userService).save(new User("firstUser", List.of("ROLE_ADMIN")));
verify(response).setStatus(HttpServletResponse.SC_OK);
}

@Test
void shouldnAuthenticateIfUserNotFound() {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
Authentication authentication = mock(Authentication.class);

when(this.userService.count()).thenReturn(1L);
when(authentication.getName()).thenReturn("newUser");
when(this.userService.findByName("newUser")).thenReturn(null);

this.successHandler.onAuthenticationSuccess(request, response, authentication);

verify(this.userService).save(new User("newUser"));
verify(response).setStatus(HttpServletResponse.SC_OK);
}

@Test
void shouldnAuthenticateIfUserAlreadyExists() {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
Authentication authentication = mock(Authentication.class);

when(this.userService.count()).thenReturn(1L);
when(authentication.getName()).thenReturn("existingUser");
when(this.userService.findByName("existingUser")).thenReturn(new User("existingUser"));

this.successHandler.onAuthenticationSuccess(request, response, authentication);

verify(this.userService, never()).save(Mockito.any(User.class));
verify(response).setStatus(HttpServletResponse.SC_OK);
}
}

0 comments on commit 9538b2f

Please sign in to comment.