use of jetbrains.buildServer.serverSide.impl.beans.VcsRootInstanceContext 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());
}
}
Aggregations