Skip to content

Commit

Permalink
Tests for controller
Browse files Browse the repository at this point in the history
  • Loading branch information
dukris committed Jan 30, 2024
1 parent 6a402e4 commit e24cc4d
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 135 deletions.
103 changes: 103 additions & 0 deletions src/test/java/git/tracehub/pmo/controller/ProjectControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2023-2024 Tracehub.git
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to read
* the Software only. Permissions is hereby NOT GRANTED to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package git.tracehub.pmo.controller;

import git.tracehub.pmo.project.Projects;
import git.tracehub.pmo.security.IdpToken;
import io.github.eocqrs.eokson.Jocument;
import io.github.eocqrs.eokson.JsonOf;
import org.cactoos.io.ResourceOf;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

/**
* Test suite for {@link IdpToken}.
*
* @since 0.0.0
*/
@ActiveProfiles("web")
@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = ProjectController.class)
final class ProjectControllerTest {

/**
* Mocked mvc.
*/
@Autowired
private MockMvc mvc;

/**
* Projects.
*/
@MockBean
@SuppressWarnings("PMD.UnusedPrivateField")
private Projects projects;

@Test
void returnsForbiddenOnUnauthorizedUser() throws Exception {
this.mvc.perform(
MockMvcRequestBuilders.post("/")
.contentType(MediaType.APPLICATION_JSON)
).andExpect(MockMvcResultMatchers.status().isForbidden());
}

@Test
void returnsProjectByUser() throws Exception {
this.mvc.perform(
MockMvcRequestBuilders.get("/")
.with(SecurityMockMvcRequestPostProcessors.jwt())
.contentType(MediaType.APPLICATION_JSON)
).andExpect(MockMvcResultMatchers.status().isOk());
}

@Test
void returnsProjectById() throws Exception {
this.mvc.perform(
MockMvcRequestBuilders.get("/74bb5ec8-0e6b-4618-bfa4-a0b76b7b312d")
.with(SecurityMockMvcRequestPostProcessors.jwt())
.contentType(MediaType.APPLICATION_JSON)
).andExpect(MockMvcResultMatchers.status().isOk());
}

@Test
void createsNewProject() throws Exception {
this.mvc.perform(
MockMvcRequestBuilders.post("/")
.with(SecurityMockMvcRequestPostProcessors.jwt())
.contentType(MediaType.APPLICATION_JSON)
.content(
new Jocument(
new JsonOf(
new ResourceOf("data/project.json").stream()
)
).toString()
)
).andExpect(MockMvcResultMatchers.status().isCreated());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/

/**
* Security Integration Tests.
* Controllers Tests.
*
* @since 0.0.0
*/
package it.security;
package git.tracehub.pmo.controller;
31 changes: 31 additions & 0 deletions src/test/java/git/tracehub/pmo/security/IpdTokenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@

package git.tracehub.pmo.security;

import com.jcabi.http.mock.MkAnswer;
import com.jcabi.http.mock.MkContainer;
import com.jcabi.http.mock.MkGrizzlyContainer;
import java.io.IOException;
import java.net.HttpURLConnection;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.Throws;
Expand All @@ -31,6 +37,31 @@
*/
final class IpdTokenTest {

@Test
void retrievesTokenSuccessfully() throws IOException {
final String expected = "token";
final MkContainer container = new MkGrizzlyContainer()
.next(
new MkAnswer.Simple(
HttpURLConnection.HTTP_OK,
"access_token=%s&expires_in=3600&token_type=bearer"
.formatted(expected)
)
).start();
final String url = container.home().toString();
final String token = new IdpToken(
Mockito.mock(Jwt.class),
"provider",
url.substring(0, url.length() - 1)
).value();
MatcherAssert.assertThat(
"Access token %s isn't correct".formatted(token),
token,
new IsEqual<>(expected)
);
container.stop();
}

@Test
@SuppressWarnings("JTCOP.RuleAssertionMessage")
void throwsOnInvalidHost() {
Expand Down
62 changes: 0 additions & 62 deletions src/test/java/it/ServerIntegration.java

This file was deleted.

71 changes: 0 additions & 71 deletions src/test/java/it/security/IdpTokenIT.java

This file was deleted.

5 changes: 5 additions & 0 deletions src/test/resources/data/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "IT project",
"active": true,
"location": "github@hizmailovich/draft:master"
}

0 comments on commit e24cc4d

Please sign in to comment.