Skip to content

Commit

Permalink
Merge pull request #97 from TAMULib/product-sprint6-95
Browse files Browse the repository at this point in the history
Issue 95: username/password for GitHub should be removed - no longer available
  • Loading branch information
kaladay committed Sep 14, 2020
2 parents 933f9d3 + fb2e4f8 commit b4a3d1f
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 41 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@
<ignore>java.util.logging.*</ignore>
<ignore>edu.tamu.app.config.AppEmailConfig</ignore>
</ignores>
<excludes>
<exclude>**/ProjectInitialization.class</exclude>
</excludes>
</instrumentation>
<formats>
<format>html</format>
Expand All @@ -173,6 +176,11 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/ProjectInitialization.class</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/edu/tamu/app/ProjectInitialization.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Component;

import edu.tamu.app.model.CardType;
import edu.tamu.app.model.ServiceType;
import edu.tamu.app.model.repo.CardTypeRepo;
import edu.tamu.app.model.repo.RemoteProjectManagerRepo;
import edu.tamu.app.service.registry.ManagementBeanRegistry;
Expand All @@ -29,6 +30,22 @@ public class ProjectInitialization implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
remoteProjectManagerRepo.findAll().forEach(versionManagementSoftware -> {
if (versionManagementSoftware.getType().equals(ServiceType.GITHUB)) {
if (!versionManagementSoftware.getSettingValue("token").isPresent()) {
return;
}
}

if (versionManagementSoftware.getType().equals(ServiceType.VERSION_ONE)) {
if (!versionManagementSoftware.getSettingValue("url").isPresent()) {
return;
}

if (versionManagementSoftware.getSettingValue("url").get().isEmpty()) {
return;
}
}

managementBeanRegistry.register(versionManagementSoftware);
});
CardType type = cardTypeRepo.findByIdentifier("Feature");
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/edu/tamu/app/model/ServiceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public List<Setting> getScaffold() {
List<Setting> scaffold = new ArrayList<Setting>();
switch (this) {
case GITHUB:
scaffold.add(new Setting("text", "url", "URL", true));
scaffold.add(new Setting("password", "token", "Token", false));
break;
case VERSION_ONE:
scaffold.add(new Setting("text", "url", "URL", true));
scaffold.add(new Setting("text", "username", "Username", false));
Expand Down
25 changes: 8 additions & 17 deletions src/main/java/edu/tamu/app/service/manager/GitHubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Map;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHIssueState;
Expand Down Expand Up @@ -40,7 +39,6 @@
import edu.tamu.app.cache.model.Sprint;
import edu.tamu.app.model.ManagementService;
import edu.tamu.app.model.request.FeatureRequest;
import edu.tamu.app.rest.BasicAuthRestTemplate;
import edu.tamu.app.rest.TokenAuthRestTemplate;

public class GitHubService extends MappingRemoteProjectManagerBean {
Expand Down Expand Up @@ -136,24 +134,18 @@ public String push(final FeatureRequest request) throws Exception {
}

protected GitHub getGitHubInstance() throws IOException {
GitHub githubInstance;
final Optional<String> endpoint = managementService.getSettingValue("url");
final Optional<String> token = managementService.getSettingValue("token");
if (!endpoint.isPresent()) {
throw new RuntimeException("GitHub service enpoint was not defined");
throw new RuntimeException("GitHub service endpoint was not defined");
}
if (token.isPresent()) {
githubInstance = ghBuilder
.withEndpoint(endpoint.get())
.withOAuthToken(token.get())
.build();
} else {
githubInstance = ghBuilder
.withEndpoint(endpoint.get())
.withPassword(getSettingValue("username"), getSettingValue("password"))
.build();
if (!token.isPresent()) {
throw new RuntimeException("GitHub token was not defined");
}
return githubInstance;
return ghBuilder
.withEndpoint(endpoint.get())
.withOAuthToken(token.get())
.build();
}

private String getSettingValue(final String key) {
Expand All @@ -166,8 +158,7 @@ private String getSettingValue(final String key) {
}

private RestTemplate getRestTemplate() {
String token = getSettingValue("token");
return StringUtils.isNotBlank(token) ? new TokenAuthRestTemplate(token) : new BasicAuthRestTemplate(getSettingValue("username"), getSettingValue("password"));
return new TokenAuthRestTemplate(getSettingValue("token"));
}

private RemoteProject buildRemoteProject(GHRepository repo, List<GHLabel> labels) throws IOException {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/edu/tamu/app/model/ModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ public abstract class ModelTest {
protected InternalRequestRepo internalRequestRepo;

protected static Map<String, String> getMockSettings() {
return getMockSettings(false);
}

protected static Map<String, String> getMockSettings(boolean token) {
if (token) {
return new HashMap<String, String>() {
private static final long serialVersionUID = 2020874481642498007L;
{
put("url", "https://localhost:9101/TexasAMLibrary");
put("token", "token");
}
};
}

return new HashMap<String, String>() {
private static final long serialVersionUID = 2020874481642498006L;
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ public class RemoteProjectManagerTest extends ModelTest {
@Test
public void testCreate() {
Map<String, String> settings = getMockSettings();
Map<String, String> settingsToken = getMockSettings(true);
RemoteProjectManager remoteProjectManager1 = remoteProjectManagerRepo.create(new RemoteProjectManager(TEST_REMOTE_PROJECT_MANAGER1_NAME, ServiceType.VERSION_ONE, settings));
RemoteProjectManager remoteProjectManager2 = remoteProjectManagerRepo.create(new RemoteProjectManager(TEST_REMOTE_PROJECT_MANAGER2_NAME, ServiceType.GITHUB, settings));
RemoteProjectManager remoteProjectManager2 = remoteProjectManagerRepo.create(new RemoteProjectManager(TEST_REMOTE_PROJECT_MANAGER2_NAME, ServiceType.GITHUB, settingsToken));
assertEquals("Remote project manager repo had incorrect number of remote project managers!", 2, remoteProjectManagerRepo.count());
assertEquals("Remote project manager had incorrect name!", TEST_REMOTE_PROJECT_MANAGER1_NAME, remoteProjectManager1.getName());
assertEquals("Remote project manager had incorrect service type!", ServiceType.VERSION_ONE, remoteProjectManager1.getType());
assertEquals("Remote project manager had incorrect settings!", settings, remoteProjectManager1.getSettings());
assertEquals("Remote project manager had incorrect name!", TEST_REMOTE_PROJECT_MANAGER2_NAME, remoteProjectManager2.getName());
assertEquals("Remote project manager had incorrect service type!", ServiceType.GITHUB, remoteProjectManager2.getType());
assertEquals("Remote project manager had incorrect settings!", settings, remoteProjectManager2.getSettings());
assertEquals("Remote project manager had incorrect settings!", settingsToken, remoteProjectManager2.getSettings());
}

@Test
Expand Down
64 changes: 42 additions & 22 deletions src/test/java/edu/tamu/app/service/manager/GitHubServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ public class GitHubServiceTest extends CacheMockTests {
@Before
public void setUp() throws Exception {
ManagementService managementService = new RemoteProjectManager("GitHub", ServiceType.GITHUB,
new HashMap<String, String>() {
private static final long serialVersionUID = 2020874481642498006L;
{
put("url", "https://localhost:9101/TexasAMLibrary");
put("username", "username");
put("password", "password");
}
});
new HashMap<String, String>() {
private static final long serialVersionUID = 2020874481642498006L;
{
put("url", "https://localhost:9101/TexasAMLibrary");
put("token", "token");
}
});


CardTypeRepo cardTypeRepo = mock(CardTypeRepo.class);
StatusRepo statusRepo = mock(StatusRepo.class);
Expand All @@ -199,7 +199,6 @@ public void setUp() throws Exception {
github = mock(GitHub.class);

when(ghBuilder.withEndpoint(any(String.class))).thenReturn(ghBuilder);
when(ghBuilder.withPassword(any(String.class), any(String.class))).thenReturn(ghBuilder);
when(ghBuilder.withOAuthToken(any(String.class))).thenReturn(ghBuilder);
when(ghBuilder.build()).thenReturn(github);

Expand Down Expand Up @@ -397,24 +396,45 @@ public void testGetMember() throws IOException {
}

@Test
public void testGetGitHubInstanceByPassword() throws IOException {
GitHub githubInstance = gitHubService.getGitHubInstance();
assertNotNull("GitHub object was not created", githubInstance);
public void testGetGitHubInstanceWithInvalidServiceEndpoint() throws IOException {
ManagementService invalidManagementService = new RemoteProjectManager("GitHub", ServiceType.GITHUB,
new HashMap<String, String>() {
private static final long serialVersionUID = 2020874481642498006L;
{
put("token", "token");
}
});

setField(gitHubService, "managementService", invalidManagementService);

try {
gitHubService.getGitHubInstance();
} catch (RuntimeException e) {
assertEquals(e.getMessage(), "GitHub service endpoint was not defined");
}
}

@Test
public void testGetGitHubInstanceByToken() throws IOException {
ManagementService tokenManagementService = new RemoteProjectManager("GitHub", ServiceType.GITHUB,
new HashMap<String, String>() {
private static final long serialVersionUID = 2020874481642498006L;
{
put("url", "https://localhost:9101/TexasAMLibrary");
put("token", "token");
}
});
public void testGetGitHubInstanceWithInvalidToken() throws IOException {
ManagementService invalidManagementService = new RemoteProjectManager("GitHub", ServiceType.GITHUB,
new HashMap<String, String>() {
private static final long serialVersionUID = 2020874481642498006L;
{
put("url", "https://localhost:9101/TexasAMLibrary");
}
});

setField(gitHubService, "managementService", invalidManagementService);

setField(gitHubService, "managementService", tokenManagementService);
try {
gitHubService.getGitHubInstance();
} catch (RuntimeException e) {
assertEquals(e.getMessage(), "GitHub token was not defined");
}
}

@Test
public void testGetGitHubInstanceByToken() throws IOException {
GitHub gitHubInstance = gitHubService.getGitHubInstance();
assertNotNull("GitHub object was not created", gitHubInstance);
}
Expand Down

0 comments on commit b4a3d1f

Please sign in to comment.