Search in sources :

Example 1 with VcsRootInstanceImpl

use of jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl in project teamcity-git by JetBrains.

the class GitHubPasswordAuthRootRegistryImplTest method test.

@Test
public void test() {
    final Map<String, Long> events = new HashMap<>();
    final MockTimeService timeService = new MockTimeService();
    final Map<String, String> vcsProperties = CollectionsUtil.asMap(VcsUtil.VCS_NAME_PROP, Constants.VCS_NAME, Constants.AUTH_METHOD, AuthenticationMethod.PASSWORD.name(), Constants.FETCH_URL, "https://github.com/name/repo.git", Constants.PASSWORD, "%some.param%");
    final Mockery mockery = new Mockery();
    final ProjectManager projectManager = mockery.mock(ProjectManager.class);
    final ServerResponsibility serverResponsibility = mockery.mock(ServerResponsibility.class);
    mockery.checking(new Expectations() {

        {
            allowing(projectManager).findVcsRootById(1);
            final MockSVcsRoot vcsRoot = new MockSVcsRoot(1, Constants.VCS_NAME);
            vcsRoot.setProperties(vcsProperties);
            will(returnValue(vcsRoot));
            allowing(serverResponsibility).canCheckForChanges();
            will(returnValue(true));
        }
    });
    final GitHubPasswordAuthRootRegistryImpl registry = new GitHubPasswordAuthRootRegistryImpl(new EventDispatcher<BuildServerListener>(BuildServerListener.class) {
    }, new EventDispatcher<RepositoryStateListener>(RepositoryStateListener.class) {
    }, projectManager, serverResponsibility, createMultiNodesEvents(events), timeService);
    vcsProperties.put(Constants.PASSWORD, "12345");
    registry.update(new VcsRootImpl(1, vcsProperties));
    assertTrue(registry.containsVcsRoot(1));
    BaseTestCase.assertMap(registry.getRegistry(), 1L, timeService.myTime);
    BaseTestCase.assertMap(events, "gitHubPasswordAuthUsageAdd", 1L);
    events.clear();
    timeService.inc();
    registry.update(new VcsRootImpl(1, vcsProperties));
    assertTrue(registry.containsVcsRoot(1));
    BaseTestCase.assertMap(registry.getRegistry(), 1L, timeService.myTime);
    assertTrue(events.isEmpty());
    timeService.inc();
    final VcsRootInstanceContext context = mockery.mock(VcsRootInstanceContext.class);
    mockery.checking(new Expectations() {

        {
            allowing(context).getTimeService();
            will(returnValue(timeService));
            allowing(context).getServerMetrics();
            will(returnValue(null));
        }
    });
    registry.update(new VcsRootInstanceImpl(100, Constants.VCS_NAME, 1, "My Name", vcsProperties, context));
    assertTrue(registry.containsVcsRoot(1));
    BaseTestCase.assertMap(registry.getRegistry(), 1L, timeService.myTime);
    assertTrue(events.isEmpty());
    timeService.inc();
    vcsProperties.put(Constants.PASSWORD, "1234567890abcdef1234567890abcdef12345678");
    registry.update(new VcsRootImpl(1, vcsProperties));
    assertTrue(registry.containsVcsRoot(1));
    BaseTestCase.assertMap(registry.getRegistry(), 1L, timeService.myTime - 1);
    assertTrue(events.isEmpty());
    setInternalProperty("teamcity.git.gitHubPasswordAuthHealthReport.updateIntervalSec", "300");
    timeService.inc(350, TimeUnit.SECONDS);
    registry.update(new VcsRootImpl(1, vcsProperties));
    assertFalse(registry.containsVcsRoot(1));
    assertTrue(registry.getRegistry().isEmpty());
    BaseTestCase.assertMap(events, "gitHubPasswordAuthUsageRemove", 1L);
    events.clear();
    timeService.inc();
    vcsProperties.put(Constants.PASSWORD, "12345");
    registry.update(new VcsRootImpl(1, vcsProperties));
    assertTrue(registry.containsVcsRoot(1));
    BaseTestCase.assertMap(registry.getRegistry(), 1L, timeService.myTime);
    BaseTestCase.assertMap(events, "gitHubPasswordAuthUsageAdd", 1L);
    events.clear();
    timeService.inc();
    // see TW-71026
    vcsProperties.put(Constants.PASSWORD, "gho_oPan0zUSMxvI7NoWDBjjBP965641HX2NHNbu");
    timeService.inc(350, TimeUnit.SECONDS);
    registry.update(new VcsRootImpl(1, vcsProperties));
    assertFalse(registry.containsVcsRoot(1));
    assertTrue(registry.getRegistry().isEmpty());
    BaseTestCase.assertMap(events, "gitHubPasswordAuthUsageRemove", 1L);
    {
        // add root to registry again
        events.clear();
        timeService.inc();
        vcsProperties.put(Constants.PASSWORD, "12345");
        registry.update(new VcsRootImpl(1, vcsProperties));
        assertTrue(registry.containsVcsRoot(1));
        BaseTestCase.assertMap(registry.getRegistry(), 1L, timeService.myTime);
        BaseTestCase.assertMap(events, "gitHubPasswordAuthUsageAdd", 1L);
    }
    {
        // TW-72780
        events.clear();
        timeService.inc();
        vcsProperties.put(Constants.AUTH_METHOD, AuthenticationMethod.ANONYMOUS.name());
        timeService.inc(350, TimeUnit.SECONDS);
        registry.update(new VcsRootImpl(1, vcsProperties));
        assertFalse(registry.containsVcsRoot(1));
        assertTrue(registry.getRegistry().isEmpty());
        BaseTestCase.assertMap(events, "gitHubPasswordAuthUsageRemove", 1L);
    }
    {
        // TW-73295
        events.clear();
        timeService.inc();
        timeService.inc(350, TimeUnit.SECONDS);
        registry.update(new VcsRootImpl(1, vcsProperties));
        assertFalse(registry.containsVcsRoot(1));
        assertTrue(registry.getRegistry().isEmpty());
        assertTrue(events.isEmpty());
    }
}
Also used : Expectations(org.jmock.Expectations) MockSVcsRoot(jetbrains.buildServer.vcs.MockSVcsRoot) HashMap(java.util.HashMap) RepositoryStateListener(jetbrains.buildServer.vcs.RepositoryStateListener) ServerResponsibility(jetbrains.buildServer.serverSide.ServerResponsibility) MockTimeService(jetbrains.buildServer.MockTimeService) BuildServerListener(jetbrains.buildServer.serverSide.BuildServerListener) Mockery(org.jmock.Mockery) GitHubPasswordAuthRootRegistryImpl(jetbrains.buildServer.buildTriggers.vcs.git.GitHubPasswordAuthRootRegistryImpl) ProjectManager(jetbrains.buildServer.serverSide.ProjectManager) VcsRootImpl(jetbrains.buildServer.vcs.impl.VcsRootImpl) VcsRootInstanceImpl(jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl) VcsRootInstanceContext(jetbrains.buildServer.serverSide.impl.beans.VcsRootInstanceContext) Test(org.testng.annotations.Test)

Example 2 with VcsRootInstanceImpl

use of jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl in project teamcity-git by JetBrains.

the class SpaceExternalChangeViewerExtensionTest method jetbrainsTeamVcsRoot.

@Test
void jetbrainsTeamVcsRoot() {
    // given
    final VcsRootInstanceImpl vcsRoot = Mockito.mock(VcsRootInstanceImpl.class);
    Mockito.doReturn(Constants.VCS_NAME).when(vcsRoot).getVcsName();
    Mockito.doReturn(selfHosted).when(vcsRoot).getProperty(Constants.FETCH_URL);
    final SProject project = Mockito.mock(SProject.class);
    final SVcsRoot sVcsRoot = Mockito.mock(SVcsRoot.class);
    Mockito.doReturn(sVcsRoot).when(vcsRoot).getParent();
    Mockito.doReturn(project).when(sVcsRoot).getProject();
    final OAuthConnectionDescriptor connection = Mockito.mock(OAuthConnectionDescriptor.class);
    Mockito.doReturn(ImmutableList.of(connection)).when(myOAuthConnectionsManager).getAvailableConnectionsOfType(project, SpaceOAuthProvider.TYPE);
    Mockito.doReturn(ImmutableMap.of(SPACE_SERVER_URL, selfHostedServerUrl, SPACE_CLIENT_ID, "ignore", SPACE_CLIENT_SECRET, "ignore")).when(connection).getParameters();
    // test
    SpaceExternalChangeViewerExtension extension = mySpaceExternalChangeViewerExtension();
    final Map<String, String> availableProperties = extension.getAvailableProperties(vcsRoot);
    assertEquals(availableProperties, ImmutableMap.of(PropertyType.CHANGE_SET_TYPE, "https://jetbrains.team/p/tc/repositories/TeamCity/revision/${changeSetId}", PropertyType.LINK_TEXT, "Open in Space", PropertyType.LINK_ICON_CLASS, "tc-icon_space"));
}
Also used : SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) VcsRootInstanceImpl(jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl) SpaceExternalChangeViewerExtension(jetbrains.buildServer.buildTriggers.vcs.git.SpaceExternalChangeViewerExtension) OAuthConnectionDescriptor(jetbrains.buildServer.serverSide.oauth.OAuthConnectionDescriptor) SProject(jetbrains.buildServer.serverSide.SProject) Test(org.testng.annotations.Test)

Example 3 with VcsRootInstanceImpl

use of jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl in project teamcity-git by JetBrains.

the class SpaceExternalChangeViewerExtensionTest method jetbrainsTeamVcsRoot_connectionNotFound.

@Test
void jetbrainsTeamVcsRoot_connectionNotFound() {
    // given
    final VcsRootInstanceImpl vcsRoot = Mockito.mock(VcsRootInstanceImpl.class);
    Mockito.doReturn(Constants.VCS_NAME).when(vcsRoot).getVcsName();
    Mockito.doReturn(selfHosted).when(vcsRoot).getProperty(Constants.FETCH_URL);
    final SProject project = Mockito.mock(SProject.class);
    final SVcsRoot sVcsRoot = Mockito.mock(SVcsRoot.class);
    Mockito.doReturn(sVcsRoot).when(vcsRoot).getParent();
    Mockito.doReturn(project).when(sVcsRoot).getProject();
    // test
    SpaceExternalChangeViewerExtension extension = mySpaceExternalChangeViewerExtension();
    final Map<String, String> availableProperties = extension.getAvailableProperties(vcsRoot);
    assertNull(availableProperties);
}
Also used : SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) VcsRootInstanceImpl(jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl) SpaceExternalChangeViewerExtension(jetbrains.buildServer.buildTriggers.vcs.git.SpaceExternalChangeViewerExtension) SProject(jetbrains.buildServer.serverSide.SProject) Test(org.testng.annotations.Test)

Example 4 with VcsRootInstanceImpl

use of jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl in project teamcity-git by JetBrains.

the class SpaceExternalChangeViewerExtensionTest method jetbrainsTeamVcsRoot_twoConnections.

@Test
void jetbrainsTeamVcsRoot_twoConnections() {
    // given
    final VcsRootInstanceImpl vcsRoot = Mockito.mock(VcsRootInstanceImpl.class);
    Mockito.doReturn(Constants.VCS_NAME).when(vcsRoot).getVcsName();
    Mockito.doReturn(selfHosted).when(vcsRoot).getProperty(Constants.FETCH_URL);
    final SProject project = Mockito.mock(SProject.class);
    final SVcsRoot sVcsRoot = Mockito.mock(SVcsRoot.class);
    Mockito.doReturn(sVcsRoot).when(vcsRoot).getParent();
    Mockito.doReturn(project).when(sVcsRoot).getProject();
    final OAuthConnectionDescriptor connection1 = Mockito.mock(OAuthConnectionDescriptor.class);
    final OAuthConnectionDescriptor connection2 = Mockito.mock(OAuthConnectionDescriptor.class);
    Mockito.doReturn(ImmutableList.of(connection1, connection2)).when(myOAuthConnectionsManager).getAvailableConnectionsOfType(project, SpaceOAuthProvider.TYPE);
    Mockito.doReturn(ImmutableMap.of(SPACE_SERVER_URL, hostedOnJetBrainsSideServerUrl, SPACE_CLIENT_ID, "ignore", SPACE_CLIENT_SECRET, "ignore")).when(connection1).getParameters();
    Mockito.doReturn(ImmutableMap.of(SPACE_SERVER_URL, selfHostedServerUrl, SPACE_CLIENT_ID, "ignore", SPACE_CLIENT_SECRET, "ignore")).when(connection2).getParameters();
    // test
    SpaceExternalChangeViewerExtension extension = mySpaceExternalChangeViewerExtension();
    final Map<String, String> availableProperties = extension.getAvailableProperties(vcsRoot);
    assertEquals(availableProperties, ImmutableMap.of(PropertyType.CHANGE_SET_TYPE, "https://jetbrains.team/p/tc/repositories/TeamCity/revision/${changeSetId}", PropertyType.LINK_TEXT, "Open in Space", PropertyType.LINK_ICON_CLASS, "tc-icon_space"));
}
Also used : SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) VcsRootInstanceImpl(jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl) SpaceExternalChangeViewerExtension(jetbrains.buildServer.buildTriggers.vcs.git.SpaceExternalChangeViewerExtension) OAuthConnectionDescriptor(jetbrains.buildServer.serverSide.oauth.OAuthConnectionDescriptor) SProject(jetbrains.buildServer.serverSide.SProject) Test(org.testng.annotations.Test)

Example 5 with VcsRootInstanceImpl

use of jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl in project teamcity-git by JetBrains.

the class SpaceExternalChangeViewerExtensionTest method jetbrainsTeamVcsRoot_urlNotFound.

@Test
void jetbrainsTeamVcsRoot_urlNotFound() {
    // given
    final VcsRootInstanceImpl vcsRoot = Mockito.mock(VcsRootInstanceImpl.class);
    Mockito.doReturn(Constants.VCS_NAME).when(vcsRoot).getVcsName();
    // test
    SpaceExternalChangeViewerExtension extension = mySpaceExternalChangeViewerExtension();
    final Map<String, String> availableProperties = extension.getAvailableProperties(vcsRoot);
    assertNull(availableProperties);
}
Also used : VcsRootInstanceImpl(jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl) SpaceExternalChangeViewerExtension(jetbrains.buildServer.buildTriggers.vcs.git.SpaceExternalChangeViewerExtension) Test(org.testng.annotations.Test)

Aggregations

VcsRootInstanceImpl (jetbrains.buildServer.vcs.impl.VcsRootInstanceImpl)8 Test (org.testng.annotations.Test)8 SpaceExternalChangeViewerExtension (jetbrains.buildServer.buildTriggers.vcs.git.SpaceExternalChangeViewerExtension)7 SProject (jetbrains.buildServer.serverSide.SProject)3 OAuthConnectionDescriptor (jetbrains.buildServer.serverSide.oauth.OAuthConnectionDescriptor)3 SVcsRoot (jetbrains.buildServer.vcs.SVcsRoot)3 HashMap (java.util.HashMap)1 MockTimeService (jetbrains.buildServer.MockTimeService)1 GitHubPasswordAuthRootRegistryImpl (jetbrains.buildServer.buildTriggers.vcs.git.GitHubPasswordAuthRootRegistryImpl)1 BuildServerListener (jetbrains.buildServer.serverSide.BuildServerListener)1 ProjectManager (jetbrains.buildServer.serverSide.ProjectManager)1 ServerResponsibility (jetbrains.buildServer.serverSide.ServerResponsibility)1 VcsRootInstanceContext (jetbrains.buildServer.serverSide.impl.beans.VcsRootInstanceContext)1 MockSVcsRoot (jetbrains.buildServer.vcs.MockSVcsRoot)1 RepositoryStateListener (jetbrains.buildServer.vcs.RepositoryStateListener)1 VcsRootImpl (jetbrains.buildServer.vcs.impl.VcsRootImpl)1 Expectations (org.jmock.Expectations)1 Mockery (org.jmock.Mockery)1