Skip to content

Commit

Permalink
Allow base64 content for create/update file (#1488)
Browse files Browse the repository at this point in the history
* Allow base64 direct content for create/update file

* Add overload to avoid breaking change
  • Loading branch information
Jérémie Bertrand authored and ryangribble committed Nov 21, 2016
1 parent 97dee1e commit 88e5342
Show file tree
Hide file tree
Showing 4 changed files with 476 additions and 15 deletions.
176 changes: 176 additions & 0 deletions Octokit.Tests.Integration/Clients/RepositoryContentsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,182 @@ await Assert.ThrowsAsync<NotFoundException>(
}
}

[IntegrationTest]
public async Task CrudTestWithExplicitBase64()
{
var client = Helper.GetAuthenticatedClient();
var fixture = client.Repository.Content;
var repoName = Helper.MakeNameWithTimestamp("source-repo");

using (var context = await client.CreateRepositoryContext(new NewRepository(repoName) { AutoInit = true }))
{
var repository = context.Repository;

var file = await fixture.CreateFile(
repository.Owner.Login,
repository.Name,
"somefile.txt",
new CreateFileRequest("Test commit", "U29tZSBDb250ZW50", false));
Assert.Equal("somefile.txt", file.Content.Name);

var contents = await fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt");
string fileSha = contents.First().Sha;
Assert.Equal("Some Content", contents.First().Content);

var update = await fixture.UpdateFile(
repository.Owner.Login,
repository.Name,
"somefile.txt",
new UpdateFileRequest("Updating file", "TmV3IENvbnRlbnQ=", fileSha, false));
Assert.Equal("somefile.txt", update.Content.Name);

contents = await fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt");
Assert.Equal("New Content", contents.First().Content);
fileSha = contents.First().Sha;

await fixture.DeleteFile(
repository.Owner.Login,
repository.Name,
"somefile.txt",
new DeleteFileRequest("Deleted file", fileSha));

await Assert.ThrowsAsync<NotFoundException>(
() => fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt"));
}
}

[IntegrationTest]
public async Task CrudTestWithRepositoryIdWithExplicitBase64()
{
var client = Helper.GetAuthenticatedClient();
var fixture = client.Repository.Content;
var repoName = Helper.MakeNameWithTimestamp("source-repo");

using (var context = await client.CreateRepositoryContext(new NewRepository(repoName) { AutoInit = true }))
{
var repository = context.Repository;

var file = await fixture.CreateFile(
repository.Id,
"somefile.txt",
new CreateFileRequest("Test commit", "U29tZSBDb250ZW50", false));
Assert.Equal("somefile.txt", file.Content.Name);

var contents = await fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt");
string fileSha = contents.First().Sha;
Assert.Equal("Some Content", contents.First().Content);

var update = await fixture.UpdateFile(
repository.Id,
"somefile.txt",
new UpdateFileRequest("Updating file", "TmV3IENvbnRlbnQ=", fileSha, false));
Assert.Equal("somefile.txt", update.Content.Name);

contents = await fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt");
Assert.Equal("New Content", contents.First().Content);
fileSha = contents.First().Sha;

await fixture.DeleteFile(
repository.Id,
"somefile.txt",
new DeleteFileRequest("Deleted file", fileSha));

await Assert.ThrowsAsync<NotFoundException>(
() => fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt"));
}
}

[IntegrationTest]
public async Task CrudTestWithNamedBranchWithExplicitBase64()
{
var client = Helper.GetAuthenticatedClient();
var fixture = client.Repository.Content;
var repoName = Helper.MakeNameWithTimestamp("source-repo");
var branchName = "other-branch";

using (var context = await client.CreateRepositoryContext(new NewRepository(repoName) { AutoInit = true }))
{
var repository = context.Repository;

var master = await client.Git.Reference.Get(Helper.UserName, repository.Name, "heads/master");
await client.Git.Reference.Create(Helper.UserName, repository.Name, new NewReference("refs/heads/" + branchName, master.Object.Sha));
var file = await fixture.CreateFile(
repository.Owner.Login,
repository.Name,
"somefile.txt",
new CreateFileRequest("Test commit", "U29tZSBDb250ZW50", branchName, false));
Assert.Equal("somefile.txt", file.Content.Name);

var contents = await fixture.GetAllContentsByRef(repository.Owner.Login, repository.Name, "somefile.txt", branchName);
string fileSha = contents.First().Sha;
Assert.Equal("Some Content", contents.First().Content);

var update = await fixture.UpdateFile(
repository.Owner.Login,
repository.Name,
"somefile.txt",
new UpdateFileRequest("Updating file", "TmV3IENvbnRlbnQ=", fileSha, branchName, false));
Assert.Equal("somefile.txt", update.Content.Name);

contents = await fixture.GetAllContentsByRef(repository.Owner.Login, repository.Name, "somefile.txt", branchName);
Assert.Equal("New Content", contents.First().Content);
fileSha = contents.First().Sha;

await fixture.DeleteFile(
repository.Owner.Login,
repository.Name,
"somefile.txt",
new DeleteFileRequest("Deleted file", fileSha, branchName));

await Assert.ThrowsAsync<NotFoundException>(
() => fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt"));
}
}

[IntegrationTest]
public async Task CrudTestWithNamedBranchWithRepositoryIdWithExplicitBase64()
{
var client = Helper.GetAuthenticatedClient();
var fixture = client.Repository.Content;
var repoName = Helper.MakeNameWithTimestamp("source-repo");
var branchName = "other-branch";

using (var context = await client.CreateRepositoryContext(new NewRepository(repoName) { AutoInit = true }))
{
var repository = context.Repository;

var master = await client.Git.Reference.Get(Helper.UserName, repository.Name, "heads/master");
await client.Git.Reference.Create(Helper.UserName, repository.Name, new NewReference("refs/heads/" + branchName, master.Object.Sha));
var file = await fixture.CreateFile(
repository.Id,
"somefile.txt",
new CreateFileRequest("Test commit", "U29tZSBDb250ZW50", branchName, false));
Assert.Equal("somefile.txt", file.Content.Name);

var contents = await fixture.GetAllContentsByRef(repository.Owner.Login, repository.Name, "somefile.txt", branchName);
string fileSha = contents.First().Sha;
Assert.Equal("Some Content", contents.First().Content);

var update = await fixture.UpdateFile(
repository.Id,
"somefile.txt",
new UpdateFileRequest("Updating file", "TmV3IENvbnRlbnQ=", fileSha, branchName, false));
Assert.Equal("somefile.txt", update.Content.Name);

contents = await fixture.GetAllContentsByRef(repository.Owner.Login, repository.Name, "somefile.txt", branchName);
Assert.Equal("New Content", contents.First().Content);
fileSha = contents.First().Sha;

await fixture.DeleteFile(
repository.Id,
"somefile.txt",
new DeleteFileRequest("Deleted file", fileSha, branchName));

await Assert.ThrowsAsync<NotFoundException>(
() => fixture.GetAllContents(repository.Owner.Login, repository.Name, "somefile.txt"));
}
}

public class TheGetArchiveMethod
{
[IntegrationTest(Skip = "this will probably take too long")]
Expand Down
122 changes: 118 additions & 4 deletions Octokit.Tests/Clients/RepositoryContentsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public async Task PassesRequestObject()
Arg.Any<Uri>(),
Arg.Is<CreateFileRequest>(a =>
a.Message == "message"
&& a.Content == "myfilecontents"
&& a.Content == "bXlmaWxlY29udGVudHM="
&& a.Branch == "mybranch"));
}

Expand All @@ -365,7 +365,63 @@ public async Task PassesRequestObjectWithRepositoryId()
Arg.Any<Uri>(),
Arg.Is<CreateFileRequest>(a =>
a.Message == "message"
&& a.Content == "myfilecontents"
&& a.Content == "bXlmaWxlY29udGVudHM="
&& a.Branch == "mybranch"));
}

[Fact]
public async Task RequestsCorrectUrlWithExplicitBase64()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

string expectedUri = "repos/org/repo/contents/path/to/file";
await client.CreateFile("org", "repo", "path/to/file", new CreateFileRequest("message", "bXlmaWxlY29udGVudHM=", "mybranch", false));

connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
}

[Fact]
public async Task RequestsCorrectUrlWithRepositoryIdWithExplicitBase64()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

string expectedUri = "repositories/1/contents/path/to/file";
await client.CreateFile(1, "path/to/file", new CreateFileRequest("message", "bXlmaWxlY29udGVudHM=", "mybranch", false));

connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
}

[Fact]
public async Task PassesRequestObjectWithExplicitBase64()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

await client.CreateFile("org", "repo", "path/to/file", new CreateFileRequest("message", "bXlmaWxlY29udGVudHM=", "mybranch", false));

connection.Received().Put<RepositoryContentChangeSet>(
Arg.Any<Uri>(),
Arg.Is<CreateFileRequest>(a =>
a.Message == "message"
&& a.Content == "bXlmaWxlY29udGVudHM="
&& a.Branch == "mybranch"));
}

[Fact]
public async Task PassesRequestObjectWithRepositoryIdWithExplicitBase64()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

await client.CreateFile(1, "path/to/file", new CreateFileRequest("message", "bXlmaWxlY29udGVudHM=", "mybranch", false));

connection.Received().Put<RepositoryContentChangeSet>(
Arg.Any<Uri>(),
Arg.Is<CreateFileRequest>(a =>
a.Message == "message"
&& a.Content == "bXlmaWxlY29udGVudHM="
&& a.Branch == "mybranch"));
}

Expand Down Expand Up @@ -509,7 +565,7 @@ public async Task PassesRequestObject()
Arg.Any<Uri>(),
Arg.Is<UpdateFileRequest>(a =>
a.Message == "message"
&& a.Content == "myfilecontents"
&& a.Content == "bXlmaWxlY29udGVudHM="
&& a.Sha == "1234abc"
&& a.Branch == "mybranch"));
}
Expand All @@ -526,7 +582,65 @@ public async Task PassesRequestObjectWithRepositoriesId()
Arg.Any<Uri>(),
Arg.Is<UpdateFileRequest>(a =>
a.Message == "message"
&& a.Content == "myfilecontents"
&& a.Content == "bXlmaWxlY29udGVudHM="
&& a.Sha == "1234abc"
&& a.Branch == "mybranch"));
}

[Fact]
public async Task RequestsCorrectUrlWithExplicitBase64()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

string expectedUri = "repos/org/repo/contents/path/to/file";
await client.UpdateFile("org", "repo", "path/to/file", new UpdateFileRequest("message", "bXlmaWxlY29udGVudHM=", "1234abc", "mybranch", false));

connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
}

[Fact]
public async Task RequestsCorrectUrlWithRepositoryIdWithExplicitBase64()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

string expectedUri = "repositories/1/contents/path/to/file";
await client.UpdateFile(1, "path/to/file", new UpdateFileRequest("message", "bXlmaWxlY29udGVudHM=", "1234abc", "mybranch", false));

connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
}

[Fact]
public async Task PassesRequestObjectWithExplicitBase64()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

await client.UpdateFile("org", "repo", "path/to/file", new UpdateFileRequest("message", "bXlmaWxlY29udGVudHM=", "1234abc", "mybranch", false));

connection.Received().Put<RepositoryContentChangeSet>(
Arg.Any<Uri>(),
Arg.Is<UpdateFileRequest>(a =>
a.Message == "message"
&& a.Content == "bXlmaWxlY29udGVudHM="
&& a.Sha == "1234abc"
&& a.Branch == "mybranch"));
}

[Fact]
public async Task PassesRequestObjectWithRepositoriesIdWithExplicitBase64()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryContentsClient(connection);

await client.UpdateFile(1, "path/to/file", new UpdateFileRequest("message", "bXlmaWxlY29udGVudHM=", "1234abc", "mybranch", false));

connection.Received().Put<RepositoryContentChangeSet>(
Arg.Any<Uri>(),
Arg.Is<UpdateFileRequest>(a =>
a.Message == "message"
&& a.Content == "bXlmaWxlY29udGVudHM="
&& a.Sha == "1234abc"
&& a.Branch == "mybranch"));
}
Expand Down
Loading

0 comments on commit 88e5342

Please sign in to comment.