using System; using System.Threading.Tasks; using NSubstitute; using Octokit.Internal; using Xunit; namespace Octokit.Tests { public class GitHubClientTests { public class TheConstructor { [Fact] public void CreatesAnonymousClientByDefault() { var client = new GitHubClient("Test Runner User Agent"); Assert.Equal(AuthenticationType.Anonymous, client.Credentials.AuthenticationType); } [Fact] public void CanCreateBasicAuthClient() { var client = new GitHubClient("Test Runner") { Credentials = new Credentials("tclem", "pwd") }; Assert.Equal(AuthenticationType.Basic, client.Credentials.AuthenticationType); } [Fact] public void CanCreateOauthClient() { var client = new GitHubClient("Test Runner") { Credentials = new Credentials("token") }; Assert.Equal(AuthenticationType.Oauth, client.Credentials.AuthenticationType); } } public class TheBaseAddressProperty { [Fact] public void IsSetToGitHubApiV3() { var client = new GitHubClient("Test Runner"); Assert.Equal(new Uri("https://api.github.com"), client.BaseAddress); } } public class TheCredentialsProperty { [Fact] public void DefaultsToAnonymous() { var client = new GitHubClient("Test Runner"); Assert.Same(Credentials.Anonymous, client.Credentials); } [Fact] public void WhenSetCreatesInMemoryStoreThatReturnsSpecifiedCredentials() { var credentials = new Credentials("Peter", "Griffin"); var client = new GitHubClient("Test Runner", Substitute.For()) { Credentials = credentials }; Assert.IsType(client.Connection.CredentialStore); Assert.Same(credentials, client.Credentials); } [Fact] public void IsRetrievedFromCredentialStore() { var credentialStore = Substitute.For(); credentialStore.GetCredentials().Returns(Task.Factory.StartNew(() => new Credentials("foo", "bar"))); var client = new GitHubClient("Test Runner", credentialStore); Assert.Equal("foo", client.Credentials.Login); Assert.Equal("bar", client.Credentials.Password); } } } }