-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented Get / IsMember for teams #449
Changes from all commits
fb6adcb
773859f
f76c74b
5f136c9
e668267
3c3c6f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
using System.Linq; | ||
using System.Net; | ||
using System.Reactive.Linq; | ||
using System.Threading.Tasks; | ||
using Octokit; | ||
using Octokit.Internal; | ||
using Octokit.Tests.Helpers; | ||
using Octokit.Tests.Integration; | ||
using Xunit; | ||
using System; | ||
using System.Collections.Generic; | ||
using Xunit.Sdk; | ||
|
||
public class TeamsClientTests | ||
{ | ||
public class TheCreateMethod | ||
{ | ||
[OrganizationTest] | ||
public async Task FailsWhenNotAuthenticated() | ||
{ | ||
var github = new GitHubClient(new ProductHeaderValue("OctokitTests")); | ||
var newTeam = new NewTeam("Test"); | ||
|
||
var e = await AssertEx.Throws<AuthorizationException>(async | ||
() => await github.Organization.Team.Create(Helper.Organization, newTeam)); | ||
|
||
Assert.Equal(HttpStatusCode.Unauthorized, e.StatusCode); | ||
} | ||
|
||
[OrganizationTest] | ||
public async Task FailsWhenAuthenticatedWithBadCredentials() | ||
{ | ||
var github = new GitHubClient(new ProductHeaderValue("OctokitTests")) | ||
{ | ||
Credentials = new Credentials(Helper.Credentials.Login, "bad-password") | ||
}; | ||
var newTeam = new NewTeam("Test"); | ||
|
||
var e = await AssertEx.Throws<AuthorizationException>(async | ||
() => await github.Organization.Team.Create(Helper.Organization, newTeam)); | ||
Assert.Equal(HttpStatusCode.Unauthorized, e.StatusCode); | ||
} | ||
|
||
[OrganizationTest] | ||
public async Task SucceedsWhenAuthenticated() | ||
{ | ||
var github = new GitHubClient(new ProductHeaderValue("OctokitTests")) | ||
{ | ||
Credentials = Helper.Credentials | ||
}; | ||
|
||
var newTeam = new NewTeam(Guid.NewGuid().ToString()); | ||
|
||
var team = await github.Organization.Team.Create(Helper.Organization, newTeam); | ||
|
||
Assert.Equal(newTeam.Name, team.Name); | ||
} | ||
} | ||
|
||
public class TheIsMemberMethod | ||
{ | ||
readonly Team team; | ||
|
||
public TheIsMemberMethod() | ||
{ | ||
var github = new GitHubClient(new ProductHeaderValue("OctokitTests")) { Credentials = Helper.Credentials }; | ||
|
||
team = github.Organization.Team.GetAll(Helper.Organization).Result.First(); | ||
} | ||
|
||
//TODO: seems like a bug in Github: it's actually returning the membership information! | ||
//Maybe because it's a public organization? | ||
//[OrganizationTest] | ||
public async Task FailsWhenNotAuthenticated() | ||
{ | ||
var github = new GitHubClient(new ProductHeaderValue("OctokitTests")); | ||
|
||
var e = await AssertEx.Throws<AuthorizationException>(async | ||
() => await github.Organization.Team.IsMember(team.Id, Helper.UserName)); | ||
|
||
Assert.Equal(HttpStatusCode.Unauthorized, e.StatusCode); | ||
} | ||
|
||
[OrganizationTest] | ||
public async Task FailsWhenAuthenticatedWithBadCredentials() | ||
{ | ||
var github = new GitHubClient(new ProductHeaderValue("OctokitTests")) | ||
{ | ||
Credentials = new Credentials(Helper.Credentials.Login, "bad-password") | ||
}; | ||
|
||
var e = await AssertEx.Throws<AuthorizationException>(async | ||
() => await github.Organization.Team.IsMember(team.Id, Helper.UserName)); | ||
Assert.Equal(HttpStatusCode.Unauthorized, e.StatusCode); | ||
} | ||
|
||
[OrganizationTest] | ||
public async Task GetsIsMemberWhenAuthenticated() | ||
{ | ||
var github = new GitHubClient(new ProductHeaderValue("OctokitTests")) | ||
{ | ||
Credentials = Helper.Credentials | ||
}; | ||
|
||
var isMember = await github.Organization.Team.IsMember(team.Id, Helper.UserName); | ||
|
||
Assert.True(isMember); | ||
} | ||
|
||
[OrganizationTest] | ||
public async Task GetsIsMemberFalseForNonMemberWhenAuthenticated() | ||
{ | ||
var github = new GitHubClient(new ProductHeaderValue("OctokitTests")) | ||
{ | ||
Credentials = Helper.Credentials | ||
}; | ||
|
||
var isMember = await github.Organization.Team.IsMember(team.Id, "foo"); | ||
|
||
Assert.False(isMember); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Net.Http.Headers; | ||
|
||
namespace Octokit.Tests.Integration | ||
|
@@ -9,6 +10,7 @@ public static class Helper | |
{ | ||
var githubUsername = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBUSERNAME"); | ||
UserName = githubUsername; | ||
Organization = Environment.GetEnvironmentVariable("OCTOKIT_GITHUBORGANIZATION"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you update the CONTRIBUTING.md docs to include this new field? Also, do you want to support running both user and org tests in the same context? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so. Even for organization tests, you always needs a valid user with ownership of the org. Team tests don't work for users since it appears only orgs can own teams (just realized of this because I got errors trying with a user, and it's not doable on the website either) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CONTRIBUTING.md doc updated! |
||
|
||
var githubToken = Environment.GetEnvironmentVariable("OCTOKIT_OAUTHTOKEN"); | ||
|
||
|
@@ -23,7 +25,16 @@ public static class Helper | |
return new Credentials(githubUsername, githubPassword); | ||
}); | ||
|
||
static Helper() | ||
{ | ||
// Force reading of environment variables. | ||
// This wasn't happening if UserName/Organization were | ||
// retrieved before Credentials. | ||
Debug.WriteIf(Credentials == null, "No credentials specified."); | ||
} | ||
|
||
public static string UserName { get; private set; } | ||
public static string Organization { get; private set; } | ||
|
||
public static Credentials Credentials { get { return _credentialsThunk.Value; }} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Collections.Generic; | ||
using Xunit.Sdk; | ||
|
||
namespace Octokit.Tests.Integration | ||
{ | ||
public class OrganizationTestAttribute : IntegrationTestAttribute | ||
{ | ||
protected override IEnumerable<ITestCommand> EnumerateTestCommands(IMethodInfo testMethod) | ||
{ | ||
if (Helper.Organization == null) | ||
return new[] | ||
{ | ||
new SkipCommand(testMethod, MethodUtility.GetDisplayName(testMethod), "Automation settings not configured. Please set the OCTOKIT_GITHUBORGANIZATION environment variable to a GitHub organization owned by the test account specified in OCTOKIT_GITHUBUSERNAME.") | ||
}; | ||
else | ||
return base.EnumerateTestCommands(testMethod); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me test to see if I can understand the why of this endpoint