Skip to content

Commit

Permalink
#61 Adding username validation & unit tests, Unpack imports where wil…
Browse files Browse the repository at this point in the history
…dcard used
  • Loading branch information
conorheffron committed Sep 14, 2024
1 parent 7678de5 commit 8ce3afb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.ironoc.portfolio.logger.AbstractLogger;
import com.ironoc.portfolio.service.GitDetailsService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -14,6 +15,7 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collections;
import java.util.List;

@Controller
Expand All @@ -38,7 +40,13 @@ public ResponseEntity<List<RepositoryDetailDomain>> getReposByUsernameReqParam(H
return getReposByUsername(request, username);
}

private ResponseEntity<List<RepositoryDetailDomain>> getReposByUsername(HttpServletRequest request, String username) {
private ResponseEntity<List<RepositoryDetailDomain>> getReposByUsername(HttpServletRequest request,
String username) {
// username validation
if (StringUtils.isBlank(username) | !StringUtils.isAlphanumeric(username) | !StringUtils.isAlpha(username)) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(Collections.emptyList());
}

info("Github get repositories by username={} for request, host={}, uri={}, user-agent={}",
username,
request.getHeader("host"),
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/ironoc/portfolio/dto/OwnerDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.Builder;
import lombok.AllArgsConstructor;

@Data
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import lombok.Builder;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Getter;
import lombok.ToString;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.emptyIterable;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.never;

@ExtendWith(MockitoExtension.class)
public class GitProjectsControllerTest {
Expand Down Expand Up @@ -45,7 +49,9 @@ public void test_getReposByUsernamePathVar_success() {
verify(gitDetailsServiceMock).mapRepositoriesToResponse(anyList());

assertThat(result, is(notNullValue()));
assertThat(result.getStatusCode(), is(HttpStatus.OK));
}

@Test
public void test_getReposByUsernameReqParam_success() {
// when
Expand All @@ -58,5 +64,22 @@ public void test_getReposByUsernameReqParam_success() {
verify(gitDetailsServiceMock).mapRepositoriesToResponse(anyList());

assertThat(result, is(notNullValue()));
assertThat(result.getStatusCode(), is(HttpStatus.OK));
}

@Test
public void test_getReposByUsernameReqParam_username_fail() {
// when
ResponseEntity<List<RepositoryDetailDomain>> result = gitProjectsController
.getReposByUsernameReqParam(httpServletRequestMock, "conor123%^*$£'");

// then
verify(httpServletRequestMock, never()).getRequestURI();
verify(httpServletRequestMock, never()).getHeader(anyString());
verify(gitDetailsServiceMock, never()).mapRepositoriesToResponse(anyList());

assertThat(result, is(notNullValue()));
assertThat(result.getBody(), is(emptyIterable()));
assertThat(result.getStatusCode(), is(HttpStatus.BAD_REQUEST));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ironoc.portfolio.client.Client;
import com.ironoc.portfolio.client.GitClient;
import com.ironoc.portfolio.config.PropertyConfigI;
import com.ironoc.portfolio.domain.RepositoryDetailDomain;
import com.ironoc.portfolio.dto.RepositoryDetailDto;
import org.junit.Ignore;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -22,11 +20,14 @@
import java.util.Optional;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.emptyString;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.ArgumentMatchers.any;

@ExtendWith(MockitoExtension.class)
public class GitDetailsServiceTest {
Expand Down

0 comments on commit 8ce3afb

Please sign in to comment.