feat: added image endpoint basic functionality
This commit is contained in:
parent
178dd05baf
commit
9c04a024c9
|
@ -0,0 +1,23 @@
|
|||
package cat.siesta.twochi_verifier_be;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
public class Controller {
|
||||
private ImageRetrieverService imageRetrieverService;
|
||||
|
||||
@GetMapping("/image")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public Map<String, Object> image() {
|
||||
var imageAndToken = imageRetrieverService.getImage();
|
||||
return Map.of("url", imageAndToken.getLeft(), "token", imageAndToken.getRight());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package cat.siesta.twochi_verifier_be;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
public interface ImageRepository {
|
||||
public URL getImage();
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package cat.siesta.twochi_verifier_be;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class ImageRetrieverService {
|
||||
private final TokenGenerator tokenGenerator;
|
||||
private final ImageRepository imageRepository;
|
||||
|
||||
public Pair<URL, String> getImage() {
|
||||
var url = imageRepository.getImage();
|
||||
return Pair.of(url, tokenGenerator.generate(url.toString()));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package cat.siesta.twochi_verifier_be;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
@EqualsAndHashCode
|
||||
@ToString
|
||||
@Getter
|
||||
@Component
|
||||
public class TokenGenerator {
|
||||
private final String secret;
|
||||
|
||||
public TokenGenerator(@Value("${twochi.secret}") String secret) {
|
||||
this.secret = secret;
|
||||
}
|
||||
|
||||
public String generate(String url) {
|
||||
return url + "\n" + secret;
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package cat.siesta.twochi_verifier_be.rest;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
||||
@RestController
|
||||
public class HomeResource {
|
||||
|
||||
@GetMapping("/")
|
||||
public String index() {
|
||||
return "\"Hello World!\"";
|
||||
}
|
||||
|
||||
}
|
|
@ -11,3 +11,5 @@ spring:
|
|||
lifecycle-management: start-only
|
||||
springdoc:
|
||||
pathsToMatch: /
|
||||
twochi:
|
||||
secret: ${TWOCHI_SECRET}
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package cat.siesta.twochi_verifier_be;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
|
||||
@SpringBootTest(properties = {
|
||||
"twochi.secret = mysecret"
|
||||
})
|
||||
@AutoConfigureMockMvc
|
||||
public class ImageRetrieverTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private TokenGenerator tokenGenerator;
|
||||
|
||||
@MockitoBean
|
||||
private ImageRepository imageRepository;
|
||||
|
||||
@Test
|
||||
void retrieves_image_with_valid_token() throws Exception {
|
||||
var url = randomUrl();
|
||||
var expectedToken = tokenGenerator.generate(url.toString());
|
||||
var expectedJson = String.format("{'url': '%s', 'token': '%s'}", url, expectedToken);
|
||||
when(imageRepository.getImage()).thenReturn(url);
|
||||
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/image"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(MockMvcResultMatchers.content().json(expectedJson));
|
||||
}
|
||||
|
||||
private URL randomUrl() {
|
||||
try {
|
||||
return URI
|
||||
.create(String.format("https://%s.com", RandomStringUtils.insecure().nextAlphabetic(10))).toURL();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("failed to create random url");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue