Search in sources :

Example 21 with Environment

use of io.gravitee.am.model.Environment in project gravitee-access-management by gravitee-io.

the class EnvironmentRepositoryTest method testFindById.

@Test
public void testFindById() {
    Environment environment = buildEnv();
    // TODO: find another way to inject data in DB. Avoid to rely on class under test for that.
    Environment envCreated = environmentRepository.create(environment).blockingGet();
    TestObserver<Environment> obs = environmentRepository.findById(envCreated.getId()).test();
    obs.awaitTerminalEvent();
    obs.assertComplete();
    obs.assertNoErrors();
    obs.assertValue(e -> e.getId().equals(envCreated.getId()));
    obs.assertValue(e -> e.getName().equals(environment.getName()));
    obs.assertValue(e -> e.getDescription().equals(environment.getDescription()));
    obs.assertValue(e -> e.getOrganizationId().equals(environment.getOrganizationId()));
    obs.assertValue(e -> e.getDomainRestrictions().containsAll(environment.getDomainRestrictions()));
    obs.assertValue(e -> e.getHrids().containsAll(environment.getHrids()));
}
Also used : Environment(io.gravitee.am.model.Environment) Test(org.junit.Test) AbstractManagementTest(io.gravitee.am.repository.management.AbstractManagementTest)

Example 22 with Environment

use of io.gravitee.am.model.Environment in project gravitee-access-management by gravitee-io.

the class EnvironmentRepositoryTest method buildEnv.

private Environment buildEnv() {
    Environment env = new Environment();
    env.setName("testName");
    env.setDescription("testDescription");
    env.setCreatedAt(new Date());
    env.setUpdatedAt(env.getUpdatedAt());
    env.setOrganizationId(UUID.randomUUID().toString());
    env.setDomainRestrictions(Arrays.asList("ValueDom1", "ValueDom2"));
    env.setHrids(Arrays.asList("Hrid1", "Hrid2"));
    return env;
}
Also used : Environment(io.gravitee.am.model.Environment) Date(java.util.Date)

Example 23 with Environment

use of io.gravitee.am.model.Environment in project gravitee-access-management by gravitee-io.

the class CockpitAuthenticationFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    if (enabled && httpRequest.getPathInfo().equals("/cockpit")) {
        String token = request.getParameter("token");
        if (StringUtils.isEmpty(token)) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST);
        } else {
            try {
                JWT jwt = jwtParser.parse(token);
                UsernamePasswordAuthenticationToken authentication = convertToAuthentication(jwt);
                User principal = authenticationService.onAuthenticationSuccess(authentication);
                final Environment environment = environmentService.findById((String) jwt.get(Claims.environment), (String) jwt.get(Claims.organization)).blockingGet();
                String redirectPath = "";
                if (environment != null) {
                    redirectPath = "/environments/" + environment.getHrids().get(0);
                }
                Cookie jwtAuthenticationCookie = jwtGenerator.generateCookie(principal);
                httpResponse.addCookie(jwtAuthenticationCookie);
                httpResponse.sendRedirect((String) jwt.get("redirect_uri") + redirectPath);
            } catch (Exception e) {
                LOGGER.error("Error occurred when trying to login using cockpit.", e);
                httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
            }
        }
    } else {
        chain.doFilter(request, response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) User(io.gravitee.am.identityprovider.api.User) JWT(io.gravitee.am.common.jwt.JWT) HttpServletResponse(javax.servlet.http.HttpServletResponse) Environment(io.gravitee.am.model.Environment) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 24 with Environment

use of io.gravitee.am.model.Environment in project gravitee-access-management by gravitee-io.

the class EnvironmentCommandHandlerTest method handle.

@Test
public void handle() {
    EnvironmentPayload environmentPayload = new EnvironmentPayload();
    EnvironmentCommand command = new EnvironmentCommand(environmentPayload);
    environmentPayload.setId("env#1");
    environmentPayload.setHrids(Collections.singletonList("env-1"));
    environmentPayload.setOrganizationId("orga#1");
    environmentPayload.setDescription("Environment description");
    environmentPayload.setName("Environment name");
    environmentPayload.setDomainRestrictions(Arrays.asList("domain.restriction1.io", "domain.restriction2.io"));
    when(environmentService.createOrUpdate(eq("orga#1"), eq("env#1"), argThat(newEnvironment -> newEnvironment.getHrids().equals(environmentPayload.getHrids()) && newEnvironment.getDescription().equals(environmentPayload.getDescription()) && newEnvironment.getName().equals(environmentPayload.getName()) && newEnvironment.getDomainRestrictions().equals(environmentPayload.getDomainRestrictions())), isNull())).thenReturn(Single.just(new Environment()));
    TestObserver<EnvironmentReply> obs = cut.handle(command).test();
    obs.awaitTerminalEvent();
    obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.SUCCEEDED));
}
Also used : EnvironmentCommand(io.gravitee.cockpit.api.command.environment.EnvironmentCommand) EnvironmentService(io.gravitee.am.service.EnvironmentService) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) Environment(io.gravitee.am.model.Environment) Mockito.when(org.mockito.Mockito.when) TechnicalException(io.gravitee.am.repository.exceptions.TechnicalException) EnvironmentPayload(io.gravitee.cockpit.api.command.environment.EnvironmentPayload) Single(io.reactivex.Single) Command(io.gravitee.cockpit.api.command.Command) EnvironmentReply(io.gravitee.cockpit.api.command.environment.EnvironmentReply) NewEnvironment(io.gravitee.am.service.model.NewEnvironment) CommandStatus(io.gravitee.cockpit.api.command.CommandStatus) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) EnvironmentCommand(io.gravitee.cockpit.api.command.environment.EnvironmentCommand) EnvironmentReply(io.gravitee.cockpit.api.command.environment.EnvironmentReply) Environment(io.gravitee.am.model.Environment) NewEnvironment(io.gravitee.am.service.model.NewEnvironment) EnvironmentPayload(io.gravitee.cockpit.api.command.environment.EnvironmentPayload) Test(org.junit.Test)

Aggregations

Environment (io.gravitee.am.model.Environment)24 Test (org.junit.Test)16 NewEnvironment (io.gravitee.am.service.model.NewEnvironment)13 Single (io.reactivex.Single)8 Collections (java.util.Collections)8 EventType (io.gravitee.am.common.audit.EventType)7 DefaultUser (io.gravitee.am.identityprovider.api.DefaultUser)7 Organization (io.gravitee.am.model.Organization)7 EnvironmentRepository (io.gravitee.am.repository.management.api.EnvironmentRepository)7 EnvironmentNotFoundException (io.gravitee.am.service.exception.EnvironmentNotFoundException)7 Flowable (io.reactivex.Flowable)7 Maybe (io.reactivex.Maybe)7 Before (org.junit.Before)7 ReferenceType (io.gravitee.am.model.ReferenceType)6 TechnicalException (io.gravitee.am.repository.exceptions.TechnicalException)6 OrganizationNotFoundException (io.gravitee.am.service.exception.OrganizationNotFoundException)6 TestObserver (io.reactivex.observers.TestObserver)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 RunWith (org.junit.runner.RunWith)6 Mock (org.mockito.Mock)6