Skip to content

Commit

Permalink
Merge pull request #3 from dmytro-ndp/qa-1108
Browse files Browse the repository at this point in the history
Get rid of default test user in selenium tests
  • Loading branch information
tolusha authored Aug 11, 2017
2 parents ef12bda + 461b386 commit 682c596
Show file tree
Hide file tree
Showing 243 changed files with 967 additions and 1,295 deletions.
4 changes: 4 additions & 0 deletions selenium/che-selenium-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,26 @@ public TestCommandServiceClient(TestApiEndpointUrlProvider apiEndpointProvider,
public void createCommand(String commandLine,
String commandName,
String commandType,
String wsId,
String accessToken) throws Exception {
String wsId) throws Exception {
CommandDto commandDto = DtoFactory.newDto(CommandDto.class);
commandDto.setName(commandName);
commandDto.setType(commandType);
commandDto.setCommandLine(commandLine);
commandDto.setAttributes(ImmutableMap.of("previewUrl", ""));
createCommand(commandDto, wsId, accessToken);
createCommand(commandDto, wsId);
}

public void createCommand(CommandDto command, String wsId, String accessToken) throws Exception {
public void createCommand(CommandDto command, String wsId) throws Exception {
requestFactory.fromUrl(apiEndpoint + "workspace/" + wsId + "/command")
.setAuthorizationHeader(accessToken)
.usePostMethod()
.setBody(command)
.request();
}


public void deleteCommand(String commandName, String wsId, String authToken) throws Exception {
public void deleteCommand(String commandName, String wsId) throws Exception {
requestFactory.fromUrl(apiEndpoint + "workspace/" + wsId + "/command/" + commandName)
.useDeleteMethod()
.setAuthorizationHeader(authToken)
.request();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ public TestFactoryServiceClient(TestApiEndpointUrlProvider testApiEndpointUrlPro
* DTO object to create the factory
* @return URL for the saved factory
*/
public String createFactory(FactoryDto createFactoryDto, String authToken) throws Exception {
public String createFactory(FactoryDto createFactoryDto) throws Exception {
HttpJsonResponse request = requestFactory.fromUrl(factoryApiEndpoint)
.setAuthorizationHeader(authToken)
.usePostMethod()
.setBody(createFactoryDto)
.request();
Expand All @@ -67,21 +66,19 @@ public String createFactory(FactoryDto createFactoryDto, String authToken) throw
return String.format("%sf?id=%s", ideUrl, responseDto.getId());
}

public String findFactoryIdByName(String authToken, String name) throws Exception {
public String findFactoryIdByName(String name) throws Exception {
String queryParamPrefix = "find?name=" + name;
HttpJsonResponse request = requestFactory.fromUrl(factoryApiEndpoint + queryParamPrefix)
.setAuthorizationHeader(authToken)
.request();
List<FactoryDto> dtos = request.asList(FactoryDto.class);
return dtos.isEmpty() ? null : dtos.get(0).getId();
}


public void deleteFactoryByName(String authToken, String name) throws Exception {
String id = findFactoryIdByName(authToken, name);
public void deleteFactoryByName(String name) throws Exception {
String id = findFactoryIdByName(name);
if (id != null) {
requestFactory.fromUrl(factoryApiEndpoint + id)
.setAuthorizationHeader(authToken)
.useDeleteMethod()
.request();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public interface TestMachineServiceClient {
/**
* Returns machine token for current workspace
*
* @param workspaceId
* the workspace id
* @param authToken
* the authorization token
* @param workspaceId
* the workspace id
* @return the machine token for current workspace
*/
String getMachineApiToken(String workspaceId, String authToken) throws Exception;
String getMachineApiToken(String workspaceId) throws Exception;
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import org.eclipse.che.api.core.rest.HttpJsonRequestFactory;
import org.eclipse.che.selenium.core.provider.TestApiEndpointUrlProvider;
import org.eclipse.che.selenium.core.user.DefaultTestUser;
import org.eclipse.che.selenium.core.user.TestUser;

import java.util.Map;

Expand All @@ -26,26 +28,29 @@
public class TestProfileServiceClient {
private final String apiEndpoint;
private final HttpJsonRequestFactory requestFactory;
private final TestUser defaultTestUser;

@Inject
public TestProfileServiceClient(TestApiEndpointUrlProvider apiEndpointProvider,
HttpJsonRequestFactory requestFactory) {
HttpJsonRequestFactory requestFactory,
DefaultTestUser defaultTestUser) {
this.apiEndpoint = apiEndpointProvider.get().toString();
this.requestFactory = requestFactory;
this.defaultTestUser = defaultTestUser;
}

public void setAttributes(Map<String, String> attributes, String authToken) throws Exception {
requestFactory.fromUrl(apiEndpoint + "profile/attributes")
.usePutMethod()
.setAuthorizationHeader(authToken)
.usePutMethod()
.setBody(attributes)
.request();
}

public void setUserNames(String name, String lastName, String authToken) throws Exception {
public void setUserNames(String name, String lastName) throws Exception {
Map<String, String> attributes = ImmutableMap.of("firstName", name,
"lastName", lastName);

setAttributes(attributes, authToken);
setAttributes(attributes, defaultTestUser.getAuthToken());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,36 @@ public TestProjectServiceClient(TestMachineServiceClient machineServiceClient,
* Set type for existing project on vfs
*/
public void setProjectType(String workspaceId,
String authToken,
String template,
String projectName) throws Exception {
InputStream in = getClass().getResourceAsStream("/templates/project/" + template);
String json = IoUtil.readAndCloseQuietly(in);

String url = getWsAgentUrl(workspaceId, authToken);
String url = getWsAgentUrl(workspaceId);
ProjectConfigDto project = getInstance().createDtoFromJson(json, ProjectConfigDto.class);

requestFactory.fromUrl(url + "/" + projectName)
.usePutMethod()
.setAuthorizationHeader(machineServiceClient.getMachineApiToken(workspaceId, authToken))
.setAuthorizationHeader(machineServiceClient.getMachineApiToken(workspaceId))
.setBody(project)
.request();
}

/**
* Delete resource.
*/
public void deleteResource(String workspaceId, String authToken, String path) throws Exception {
String wsAgentUrl = getWsAgentUrl(workspaceId, authToken);
public void deleteResource(String workspaceId, String path) throws Exception {
String wsAgentUrl = getWsAgentUrl(workspaceId);
requestFactory.fromUrl(wsAgentUrl + "/" + path)
.setAuthorizationHeader(machineServiceClient.getMachineApiToken(workspaceId, authToken))
.setAuthorizationHeader(machineServiceClient.getMachineApiToken(workspaceId))
.useDeleteMethod()
.request();
}

public void createFolder(String workspaceId, String authToken, String folder) throws Exception {
String url = getWsAgentUrl(workspaceId, authToken) + "/folder/" + folder;
public void createFolder(String workspaceId, String folder) throws Exception {
String url = getWsAgentUrl(workspaceId) + "/folder/" + folder;
requestFactory.fromUrl(url)
.setAuthorizationHeader(machineServiceClient.getMachineApiToken(workspaceId, authToken))
.setAuthorizationHeader(machineServiceClient.getMachineApiToken(workspaceId))
.usePostMethod()
.request();
}
Expand All @@ -96,19 +95,18 @@ public void createFolder(String workspaceId, String authToken, String folder) th
* Import zip project from file system into user workspace.
*/
public void importZipProject(String workspaceId,
String authToken,
Path zipFile,
String projectName,
String template) throws Exception {
String url = getWsAgentUrl(workspaceId, authToken) + "/import/" + projectName;
createFolder(workspaceId, authToken, projectName);
String url = getWsAgentUrl(workspaceId) + "/import/" + projectName;
createFolder(workspaceId, projectName);

HttpURLConnection httpConnection = null;
try {
httpConnection = (HttpURLConnection)new URL(url).openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type", "application/zip");
httpConnection.addRequestProperty("Authorization", machineServiceClient.getMachineApiToken(workspaceId, authToken));
httpConnection.addRequestProperty("Authorization", machineServiceClient.getMachineApiToken(workspaceId));
httpConnection.setDoOutput(true);

try (OutputStream outputStream = httpConnection.getOutputStream()) {
Expand All @@ -124,15 +122,14 @@ public void importZipProject(String workspaceId,
ofNullable(httpConnection).ifPresent(HttpURLConnection::disconnect);
}

setProjectType(workspaceId, authToken, template, projectName);
setProjectType(workspaceId, template, projectName);
}


/**
* Import project from file system into a user workspace
*/
public void importProject(String workspaceId,
String authToken,
Path sourceFolder,
String projectName,
String template) throws Exception {
Expand All @@ -148,26 +145,25 @@ public void importProject(String workspaceId,
Path zip = Files.createTempFile("project", projectName);
ZipUtils.zipDir(sourceFolder.toString(), sourceFolder.toFile(), zip.toFile(), null);

importZipProject(workspaceId, authToken, zip, projectName, template);
importZipProject(workspaceId, zip, projectName, template);
}


/**
* Creates file in the project.
*/
public void createFileInProject(String workspaceId,
String authToken,
String parentFolder,
String fileName,
String content) throws Exception {
String apiRESTUrl = getWsAgentUrl(workspaceId, authToken) + "/file/" + parentFolder + "?name=" + fileName;
String apiRESTUrl = getWsAgentUrl(workspaceId) + "/file/" + parentFolder + "?name=" + fileName;

HttpURLConnection httpConnection = null;
try {
httpConnection = (HttpURLConnection)new URL(apiRESTUrl).openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type", "text/plain");
httpConnection.addRequestProperty("Authorization", machineServiceClient.getMachineApiToken(workspaceId, authToken));
httpConnection.addRequestProperty("Authorization", machineServiceClient.getMachineApiToken(workspaceId));
httpConnection.setDoOutput(true);
try (OutputStream output = httpConnection.getOutputStream()) {
output.write(content.getBytes("UTF-8"));
Expand All @@ -183,10 +179,10 @@ public void createFileInProject(String workspaceId,
}
}

public ProjectConfig getFirstProject(String workspaceId, String authToken) throws Exception {
String apiUrl = getWsAgentUrl(workspaceId, authToken);
public ProjectConfig getFirstProject(String workspaceId) throws Exception {
String apiUrl = getWsAgentUrl(workspaceId);
return requestFactory.fromUrl(apiUrl)
.setAuthorizationHeader(machineServiceClient.getMachineApiToken(workspaceId, authToken))
.setAuthorizationHeader(machineServiceClient.getMachineApiToken(workspaceId))
.request()
.asList(ProjectConfigDto.class)
.get(0);
Expand All @@ -196,17 +192,16 @@ public ProjectConfig getFirstProject(String workspaceId, String authToken) throw
* Updates file content.
*/
public void updateFile(String workspaceId,
String authToken,
String pathToFile,
String content) throws Exception {
String url = getWsAgentUrl(workspaceId, authToken) + "/file/" + pathToFile;
String url = getWsAgentUrl(workspaceId) + "/file/" + pathToFile;

HttpURLConnection httpConnection = null;
try {
httpConnection = (HttpURLConnection)new URL(url).openConnection();
httpConnection.setRequestMethod("PUT");
httpConnection.setRequestProperty("Content-Type", "text/plain");
httpConnection.addRequestProperty("Authorization", machineServiceClient.getMachineApiToken(workspaceId, authToken));
httpConnection.addRequestProperty("Authorization", machineServiceClient.getMachineApiToken(workspaceId));
httpConnection.setDoOutput(true);

try (OutputStream output = httpConnection.getOutputStream()) {
Expand All @@ -223,8 +218,8 @@ public void updateFile(String workspaceId,
}
}

private String getWsAgentUrl(String workspaceId, String authToken) throws Exception {
Workspace workspace = workspaceServiceClient.getById(workspaceId, authToken);
private String getWsAgentUrl(String workspaceId) throws Exception {
Workspace workspace = workspaceServiceClient.getById(workspaceId);
workspaceServiceClient.ensureRunningStatus(workspace);

return workspace.getRuntime()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
*/
@Singleton
public class TestSshServiceClient {
private final String apiEndpoint;
private static final String MACHINE_SERVICE = "machine";
private static final String VCS_SERVICE = "vcs";
private final String apiEndpoint;
private final HttpJsonRequestFactory requestFactory;

@Inject
Expand All @@ -39,31 +41,28 @@ public TestSshServiceClient(TestApiEndpointUrlProvider apiEndpointProvider,
}


public String getPrivateKeyByName(String authToken, String key) throws Exception {
HttpJsonResponse request = requestFactory.fromUrl(apiEndpoint + "ssh/machine/" + "?name=" + key)
.setAuthorizationHeader(authToken)
public String getPrivateKeyByName(String name) throws Exception {
HttpJsonResponse request = requestFactory.fromUrl(String.format("%sssh/%s/?name=%s", apiEndpoint, MACHINE_SERVICE, name))
.useGetMethod()
.request();
List<SshPairDto> sshPair = request.asList(SshPairDto.class);
return sshPair.isEmpty() ? null : sshPair.get(0).getPrivateKey();
}


public void deleteMachineKeyByName(String authToken, String key) throws Exception {
requestFactory.fromUrl(apiEndpoint + "ssh/machine/" + "?name=" + key)
.setAuthorizationHeader(authToken)
public void deleteMachineKeyByName(String name) throws Exception {
requestFactory.fromUrl(apiEndpoint + "ssh/" + MACHINE_SERVICE + "/?name=" + name)
.useDeleteMethod()
.request();
}

public String generateSshKeys(String authToken) throws Exception {
public String generateGithubKey() throws Exception {
GenerateSshPairRequest generateSshKeyData = newDto(GenerateSshPairRequest.class)
.withName("github.com")
.withService("vcs");
.withService(VCS_SERVICE);

HttpJsonResponse response = requestFactory.fromUrl(apiEndpoint + "ssh/generate")
.usePostMethod()
.setAuthorizationHeader(authToken)
.setBody(generateSshKeyData)
.request();
return response.asDto(SshPairDto.class).getPublicKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ public TestUserPreferencesServiceClient(TestApiEndpointUrlProvider apiEndpointPr
this.httpRequestFactory = httpRequestFactory;
}

public void addGitCommitter(String authToken, String committerName, String committerEmail) throws Exception {
public void addGitCommitter(String committerName, String committerEmail) throws Exception {
httpRequestFactory.fromUrl(apiEndpoint + "preferences")
.setAuthorizationHeader(authToken)
.usePutMethod()
.setBody(ImmutableMap.of("git.committer.name", committerName,
"git.committer.email", committerEmail))
Expand Down
Loading

0 comments on commit 682c596

Please sign in to comment.