Files
github-api/src/test/java/org/kohsuke/github/GHContentIntegrationTest.java
Alex Taylor 4c3a0d329b added Authentication Check
Added additional authentication checks on gitHubBeforeAfter so that cleanup is done with a user logged in
2020-01-27 15:24:28 -05:00

167 lines
6.7 KiB
Java

package org.kohsuke.github;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Integration test for {@link GHContent}.
*/
public class GHContentIntegrationTest extends AbstractGitHubWireMockTest {
private GHRepository repo;
// file name with spaces and other chars
private final String createdDirectory = "test+directory #50";
private final String createdFilename = createdDirectory + "/test file-to+create-#1.txt";
@Before
@After
public void cleanup() throws Exception {
if (mockGitHub.isUseProxy()) {
repo = getGitHubBeforeAfter().getRepository("github-api-test-org/GHContentIntegrationTest");
try {
GHContent content = repo.getFileContent(createdFilename);
if (content != null) {
content.delete("Cleanup");
}
} catch (IOException e) {
}
}
}
@Before
public void setUp() throws Exception {
repo = gitHub.getRepository("github-api-test-org/GHContentIntegrationTest");
}
@Test
public void testGetFileContent() throws Exception {
repo = gitHub.getRepository("github-api-test-org/GHContentIntegrationTest");
GHContent content = repo.getFileContent("ghcontent-ro/a-file-with-content");
assertTrue(content.isFile());
assertEquals("thanks for reading me\n", content.getContent());
}
@Test
public void testGetEmptyFileContent() throws Exception {
GHContent content = repo.getFileContent("ghcontent-ro/an-empty-file");
assertTrue(content.isFile());
assertEquals("", content.getContent());
}
@Test
public void testGetDirectoryContent() throws Exception {
List<GHContent> entries = repo.getDirectoryContent("ghcontent-ro/a-dir-with-3-entries");
assertTrue(entries.size() == 3);
}
@Test
public void testGetDirectoryContentTrailingSlash() throws Exception {
// Used to truncate the ?ref=master, see gh-224 https://github.com/kohsuke/github-api/pull/224
List<GHContent> entries = repo.getDirectoryContent("ghcontent-ro/a-dir-with-3-entries/", "master");
assertTrue(entries.get(0).getUrl().endsWith("?ref=master"));
}
@Test
public void testCRUDContent() throws Exception {
GHContentUpdateResponse created = repo.createContent("this is an awesome file I created\n",
"Creating a file for integration tests.",
createdFilename);
GHContent createdContent = created.getContent();
assertNotNull(created.getCommit());
assertNotNull(created.getContent());
assertNotNull(createdContent.getContent());
assertThat(createdContent.getPath(), equalTo(createdFilename));
assertEquals("this is an awesome file I created\n", createdContent.getContent());
GHContent content = repo.getFileContent(createdFilename);
assertThat(content, is(notNullValue()));
assertThat(content.getSha(), equalTo(createdContent.getSha()));
assertThat(content.getContent(), equalTo(createdContent.getContent()));
assertThat(content.getPath(), equalTo(createdContent.getPath()));
List<GHContent> directoryContents = repo.getDirectoryContent(createdDirectory);
assertThat(directoryContents, is(notNullValue()));
assertThat(directoryContents.size(), equalTo(1));
content = directoryContents.get(0);
assertThat(content.getSha(), is(created.getContent().getSha()));
assertThat(content.getContent(), is(created.getContent().getContent()));
assertThat(content.getPath(), equalTo(createdFilename));
GHContentUpdateResponse updatedContentResponse = createdContent.update("this is some new content\n",
"Updated file for integration tests.");
GHContent updatedContent = updatedContentResponse.getContent();
assertNotNull(updatedContentResponse.getCommit());
assertNotNull(updatedContentResponse.getContent());
// due to what appears to be a cache propagation delay, this test is too flaky
assertEquals("this is some new content",
new BufferedReader(new InputStreamReader(updatedContent.read())).readLine());
assertEquals("this is some new content\n", updatedContent.getContent());
GHContentUpdateResponse deleteResponse = updatedContent.delete("Enough of this foolishness!");
assertNotNull(deleteResponse.getCommit());
assertNull(deleteResponse.getContent());
try {
repo.getFileContent(createdFilename);
fail("Delete didn't work!");
} catch (GHFileNotFoundException e) {
assertThat(e.getMessage(),
equalTo("{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/repos/contents/#get-contents\"}"));
}
}
@Test
public void testMIMESmall() throws IOException {
GHRepository ghRepository = getTempRepository();
GHContentBuilder ghContentBuilder = ghRepository.createContent();
ghContentBuilder.message("Some commit msg");
ghContentBuilder.path("MIME-Small.md");
ghContentBuilder.content("123456789012345678901234567890123456789012345678901234567");
ghContentBuilder.commit();
}
@Test
public void testMIMELong() throws IOException {
GHRepository ghRepository = getTempRepository();
GHContentBuilder ghContentBuilder = ghRepository.createContent();
ghContentBuilder.message("Some commit msg");
ghContentBuilder.path("MIME-Long.md");
ghContentBuilder.content("1234567890123456789012345678901234567890123456789012345678");
ghContentBuilder.commit();
}
@Test
public void testMIMELonger() throws IOException {
GHRepository ghRepository = getTempRepository();
GHContentBuilder ghContentBuilder = ghRepository.createContent();
ghContentBuilder.message("Some commit msg");
ghContentBuilder.path("MIME-Long.md");
ghContentBuilder.content("123456789012345678901234567890123456789012345678901234567890"
+ "123456789012345678901234567890123456789012345678901234567890"
+ "123456789012345678901234567890123456789012345678901234567890"
+ "123456789012345678901234567890123456789012345678901234567890");
ghContentBuilder.commit();
}
}