use of org.finos.legend.sdlc.server.gitlab.auth.TestGitLabSession 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);
}
Aggregations