Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jan 31, 2024
2 parents 441de4f + 2f2eb7c commit 748f413
Show file tree
Hide file tree
Showing 12 changed files with 382 additions and 29 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ SOFTWARE.
<jcabi-http.version>1.20.1</jcabi-http.version>
<testcontainers-keycloak.version>3.2.0</testcontainers-keycloak.version>
<testcontainers.version>1.19.3</testcontainers.version>
<grizzly.version>4.0.2</grizzly.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -153,6 +154,12 @@ SOFTWARE.
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-server</artifactId>
<version>${grizzly.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package git.tracehub.pmo.controller;

import com.jcabi.github.RtGithub;
import git.tracehub.pmo.platforms.RepoPath;
import git.tracehub.pmo.platforms.github.InviteCollaborator;
import git.tracehub.pmo.project.Project;
import git.tracehub.pmo.project.Projects;
Expand Down Expand Up @@ -106,9 +108,11 @@ public Project employ(
*/
if (new ExistsRole(jwt, "user_github").value()) {
new InviteCollaborator(
created.getLocation(),
new RepoPath(created.getLocation()).value(),
"tracehubgit",
new IdpToken(jwt, "github", this.url).value()
new RtGithub(
new IdpToken(jwt, "github", this.url).value()
)
).exec();
}
return created;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
package git.tracehub.pmo.platforms.github;

import com.jcabi.github.Coordinates;
import com.jcabi.github.RtGithub;
import com.jcabi.github.Github;
import git.tracehub.pmo.platforms.Action;
import git.tracehub.pmo.platforms.RepoPath;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;

Expand All @@ -43,18 +42,16 @@ public final class InviteCollaborator implements Action {
private final String username;

/**
* Token.
* Github.
*/
private final String token;
private final Github github;

@Override
@SneakyThrows
public void exec() {
new RtGithub(this.token).repos()
this.github.repos()
.get(
new Coordinates.Simple(
new RepoPath(this.location).value()
)
new Coordinates.Simple(this.location)
).collaborators()
.add(this.username);
}
Expand Down
19 changes: 8 additions & 11 deletions src/main/java/git/tracehub/pmo/security/IdpToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.jcabi.http.Request;
import com.jcabi.http.request.JdkRequest;
import com.jcabi.http.response.RestResponse;
import java.net.HttpURLConnection;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.cactoos.Scalar;
Expand Down Expand Up @@ -54,15 +55,8 @@ public final class IdpToken implements Scalar<String> {
@Override
@SneakyThrows
public String value() {
/*
* @todo #1:45min/DEV fix 403 Forbidden error when trying to get
* token from IDP. It seems that the user hasn't enough permissions
* to get the token from IDP. We need to configure Keycloak to allow
* the user to read the token. See the following link for more info:
* https://www.keycloak.org/docs/latest/server_admin/#retrieving-external-idp-tokens
*/
new JdkRequest(
"%s//broker/%s/token".formatted(
return new JdkRequest(
"%s/broker/%s/token".formatted(
this.url,
this.provider
)
Expand All @@ -72,8 +66,11 @@ public String value() {
HttpHeaders.AUTHORIZATION,
"Bearer %s".formatted(this.jwt.getTokenValue())
).fetch()
.as(RestResponse.class);
return null;
.as(RestResponse.class)
.assertStatus(HttpURLConnection.HTTP_OK)
.body()
.split("&")[0]
.split("=")[1];
}

}
102 changes: 102 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,102 @@
/*
* 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 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 ProjectController}.
*
* @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());
}

}
23 changes: 23 additions & 0 deletions src/test/java/git/tracehub/pmo/controller/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.
*/

/**
* Controllers Tests.
*
* @since 0.0.0
*/
package git.tracehub.pmo.controller;
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.platforms.github;

import com.jcabi.github.Repo;
import com.jcabi.github.Repos;
import com.jcabi.github.mock.MkGithub;
import java.io.IOException;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Test suite for {@link InviteCollaborator}.
*
* @since 0.0.0
*/
final class InviteCollaboratorTest {

@Test
void invitesCollaboratorSuccessfully() throws IOException {
final String collaborator = "name";
final MkGithub github = new MkGithub("user");
final Repo repo = github.repos().create(
new Repos.RepoCreate("repo", false)
);
new InviteCollaborator("user/repo", collaborator, github).exec();
MatcherAssert.assertThat(
"Collaborator %s isn't invited as expected"
.formatted(collaborator),
repo.collaborators().isCollaborator(collaborator),
new IsEqual<>(true)
);
}

@Test
void trowsOnInvalidLocation() {
Assertions.assertThrows(
IllegalArgumentException.class,
() -> new InviteCollaborator("user", "user", new MkGithub("user"))
.exec(),
"Exception is not thrown or valid"
);
}

}
23 changes: 23 additions & 0 deletions src/test/java/git/tracehub/pmo/platforms/github/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.
*/

/**
* Github Platform Tests.
*
* @since 0.0.0
*/
package git.tracehub.pmo.platforms.github;
Loading

1 comment on commit 748f413

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 748f413 Jan 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1-ecd686dc disappeared from src/main/java/git/tracehub/pmo/security/IdpToken.java), that's why I closed #17. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.