Search in sources :

Example 11 with GitLabMode

use of org.finos.legend.sdlc.server.gitlab.mode.GitLabMode in project legend-sdlc by finos.

the class TestGitLabProjectId method testEquality.

@Test
public void testEquality() {
    GitLabMode[] modes = GitLabMode.values();
    for (GitLabMode mode : modes) {
        for (int i = 0; i < 1024; i++) {
            GitLabProjectId projectId = GitLabProjectId.newProjectId(mode, i);
            Assert.assertEquals(projectId, projectId);
            Assert.assertEquals(projectId, GitLabProjectId.newProjectId(mode, i));
            Assert.assertNotEquals(projectId, GitLabProjectId.newProjectId(mode, i + 1));
            Assert.assertNotEquals(projectId, GitLabProjectId.newProjectId(mode, i - 1));
            Assert.assertNotEquals(projectId, GitLabProjectId.newProjectId(modes[(mode.ordinal() + 1) % modes.length], i));
        }
    }
}
Also used : GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode) Test(org.junit.Test)

Example 12 with GitLabMode

use of org.finos.legend.sdlc.server.gitlab.mode.GitLabMode in project legend-sdlc by finos.

the class GitLabApiTestSetupUtil method prepareGitLabUserContextHelper.

/**
 * Authenticates to GitLab and creates a test GitLabUserContext.
 *
 * @param username   the name of user for whom we create this context.
 * @param password   the password of user for whom we create this context.
 * @param hostUrl    the url of the test host.
 * @param hostScheme the scheme of the test host.
 * @param hostHost   the test host.
 * @param hostPort   the port (if necessary) of the test host.
 */
public static GitLabUserContext prepareGitLabUserContextHelper(String username, String password, String hostUrl, String hostScheme, String hostHost, Integer hostPort) throws LegendSDLCServerException {
    GitLabMode gitLabMode = GitLabMode.PROD;
    TestHttpServletRequest httpServletRequest = new TestHttpServletRequest();
    TestGitLabSession session = new TestGitLabSession(username);
    GitLabApi oauthGitLabApi;
    Version version;
    try {
        oauthGitLabApi = GitLabApi.oauth2Login(hostUrl, username, password, null, null, true);
        Assert.assertNotNull(oauthGitLabApi);
        version = oauthGitLabApi.getVersion();
    } catch (GitLabApiException e) {
        StringBuilder builder = new StringBuilder("Error instantiating GitLabApi via OAuth2; response status: ").append(e.getHttpStatus());
        StringTools.appendThrowableMessageIfPresent(builder, e, "; error message: ");
        if (e.hasValidationErrors()) {
            builder.append("; validation error(s): ").append(e.getValidationErrors());
        }
        throw new LegendSDLCServerException(builder.toString(), e);
    }
    String oauthToken = oauthGitLabApi.getAuthToken();
    LOGGER.info("Retrieved access token: {}", oauthToken);
    Assert.assertNotNull(version);
    GitLabServerInfo gitLabServerInfo = GitLabServerInfo.newServerInfo(hostScheme, hostHost, hostPort);
    GitLabAppInfo gitLabAppInfo = GitLabAppInfo.newAppInfo(gitLabServerInfo, null, null, null);
    GitLabModeInfo gitLabModeInfo = GitLabModeInfo.newModeInfo(gitLabMode, gitLabAppInfo);
    session.setGitLabToken(gitLabMode, oauthToken, TokenType.OAUTH2_ACCESS);
    session.setModeInfo(gitLabModeInfo);
    LegendSDLCWebFilter.setSessionAttributeOnServletRequest(httpServletRequest, session);
    GitLabAuthorizerManager authorizerManager = GitLabAuthorizerManager.newManager(Collections.emptyList());
    return new GitLabUserContext(httpServletRequest, null, authorizerManager);
}
Also used : GitLabServerInfo(org.finos.legend.sdlc.server.gitlab.GitLabServerInfo) GitLabApi(org.gitlab4j.api.GitLabApi) GitLabModeInfo(org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo) GitLabAuthorizerManager(org.finos.legend.sdlc.server.gitlab.auth.GitLabAuthorizerManager) GitLabApiException(org.gitlab4j.api.GitLabApiException) TestGitLabSession(org.finos.legend.sdlc.server.gitlab.auth.TestGitLabSession) GitLabAppInfo(org.finos.legend.sdlc.server.gitlab.GitLabAppInfo) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Version(org.gitlab4j.api.models.Version) GitLabUserContext(org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)

Example 13 with GitLabMode

use of org.finos.legend.sdlc.server.gitlab.mode.GitLabMode in project legend-sdlc by finos.

the class GitLabTokenManager method putAllFromToken.

void putAllFromToken(Token.TokenReader reader) {
    synchronized (this.tokens) {
        for (int size = reader.getInt(); size > 0; size--) {
            // Read values
            String modeName = reader.getString();
            String appId = reader.getString();
            String typeName = reader.getString();
            String token = reader.getString();
            if ((modeName != null) && (appId != null) && (typeName != null) && (token != null)) {
                // Get mode and token type
                GitLabMode mode;
                TokenType type;
                try {
                    mode = GitLabMode.valueOf(modeName);
                    type = TokenType.valueOf(typeName);
                } catch (IllegalArgumentException e) {
                    // unknown mode or token type - token will be ignored
                    continue;
                }
                // Check the mode info
                GitLabModeInfo modeInfo = this.modeInfos.getModeInfo(mode);
                if ((modeInfo != null) && appId.equals(modeInfo.getAppInfo().getAppId())) {
                    this.tokens.put(mode, GitLabToken.newGitLabToken(type, token));
                }
            }
        }
    }
}
Also used : TokenType(org.gitlab4j.api.Constants.TokenType) GitLabModeInfo(org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)

Example 14 with GitLabMode

use of org.finos.legend.sdlc.server.gitlab.mode.GitLabMode in project legend-sdlc by finos.

the class TestGitLabMode method testGetMode_CaseSensitive.

@Test
public void testGetMode_CaseSensitive() {
    for (GitLabMode mode : GitLabMode.values()) {
        Assert.assertEquals(mode, GitLabMode.getMode(mode.name(), true));
        try {
            GitLabMode.getMode(mode.name().toLowerCase(), true);
            Assert.fail("Expected exception: " + mode.name().toLowerCase());
        } catch (IllegalArgumentException e) {
            Assert.assertEquals("Unknown mode: " + mode.name().toLowerCase(), e.getMessage());
        }
    }
}
Also used : GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode) Test(org.junit.Test)

Example 15 with GitLabMode

use of org.finos.legend.sdlc.server.gitlab.mode.GitLabMode in project legend-sdlc by finos.

the class TestGitLabMode method testGetMode_CaseInsensitive.

@Test
public void testGetMode_CaseInsensitive() {
    for (GitLabMode mode : GitLabMode.values()) {
        Assert.assertEquals(mode, GitLabMode.getMode(mode.name(), false));
        Assert.assertEquals(mode, GitLabMode.getMode(mode.name().toLowerCase(), false));
    }
    Assert.assertEquals(GitLabMode.PROD, GitLabMode.getMode("pRoD", false));
    Assert.assertEquals(GitLabMode.UAT, GitLabMode.getMode("UAt", false));
}
Also used : GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode) Test(org.junit.Test)

Aggregations

GitLabMode (org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)15 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)7 Test (org.junit.Test)4 GitLabApi (org.gitlab4j.api.GitLabApi)3 Project (org.finos.legend.sdlc.domain.model.project.Project)2 User (org.finos.legend.sdlc.domain.model.user.User)2 GitLabUserContext (org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext)2 GitLabModeInfo (org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo)2 ProjectStructure (org.finos.legend.sdlc.server.project.ProjectStructure)2 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 EnumSet (java.util.EnumSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 Inject (javax.inject.Inject)1