@Test
public void authenticateShouldSynchronizeTeamsWhenUserAlreadyExistsAndTeamSynchronizationIsEnabled() throws AlpineAuthenticationException {

    when(configMock.getPropertyAsBoolean(eq(Config.AlpineKey.OIDC_TEAM_SYNCHRONIZATION)))
            .thenReturn(true);
    when(configMock.getProperty(eq(Config.AlpineKey.OIDC_TEAMS_CLAIM)))
            .thenReturn("groups");
    wireMockRule.stubFor(get(urlPathEqualTo(OIDC_USERINFO_PATH))
            .willReturn(aResponse()
                    .withStatus(HttpStatus.SC_OK)
                    .withHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType())
                    .withBody("" +
                            "{" +
                            "    \"sub\": \"subject\", " +
                            "    \"email\": \"subject@mail.local\", " +
                            "    \"" + USERNAMCE_CLAIM + "\": \"username\", " +
                            "    \"groups\": [\"groupName\"]" +
                            "}")));
    Team teamToSync;
    try (final AlpineQueryManager qm = new AlpineQueryManager()) {
        final OidcUser existingUser = new OidcUser();
        existingUser.setUsername("username");
        existingUser.setSubjectIdentifier("subject");
        existingUser.setEmail("subject@mail.local");
        qm.persist(existingUser);
        OidcGroup group = new OidcGroup();
        group.setName("groupName");
        group = qm.persist(group);
        teamToSync = new Team();
        teamToSync.setName("teamName");
        teamToSync = qm.persist(teamToSync);
        MappedOidcGroup mappedGroup = new MappedOidcGroup();
        mappedGroup.setGroup(group);
        mappedGroup.setTeam(teamToSync);
        qm.persist(mappedGroup);
    }
    final OidcAuthenticationService authService = new OidcAuthenticationService(configMock, oidcConfigurationMock, ACCESS_TOKEN);
    final OidcUser authenticatedUser = (OidcUser) authService.authenticate();
    assertThat(authenticatedUser.getTeams()).hasSize(1);
    assertThat(authenticatedUser.getTeams().get(0).getName()).isEqualTo("teamName");
}