forked from octokit/octokit.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitHubClientTests.cs
215 lines (180 loc) · 8.87 KB
/
GitHubClientTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System;
using System.Threading.Tasks;
using NSubstitute;
using Octokit.Internal;
using Xunit;
using System.Collections.Generic;
namespace Octokit.Tests
{
public class GitHubClientTests
{
public class TheCtor
{
[Fact]
public void EnsuresNonNullArguments()
{
Assert.Throws<ArgumentNullException>(() => new GitHubClient((IConnection)null));
Assert.Throws<ArgumentNullException>(() => new GitHubClient((ProductHeaderValue)null));
var productInformation = new ProductHeaderValue("UnitTest");
var baseAddress = new Uri("http://github.com");
var credentialStore = Substitute.For<ICredentialStore>();
Assert.Throws<ArgumentNullException>(() => new GitHubClient(productInformation, (ICredentialStore)null));
Assert.Throws<ArgumentNullException>(() => new GitHubClient(null, credentialStore));
Assert.Throws<ArgumentNullException>(() => new GitHubClient(productInformation, (Uri)null));
Assert.Throws<ArgumentNullException>(() => new GitHubClient(null, baseAddress));
Assert.Throws<ArgumentNullException>(() => new GitHubClient(null, (ICredentialStore)null));
Assert.Throws<ArgumentNullException>(() => new GitHubClient(null, (Uri)null));
Assert.Throws<ArgumentNullException>(() => new GitHubClient(productInformation, null, null));
Assert.Throws<ArgumentNullException>(() => new GitHubClient(null, credentialStore, null));
Assert.Throws<ArgumentNullException>(() => new GitHubClient(null, null, baseAddress));
Assert.Throws<ArgumentNullException>(() => new GitHubClient(null, credentialStore, baseAddress));
Assert.Throws<ArgumentNullException>(() => new GitHubClient(productInformation, null, baseAddress));
Assert.Throws<ArgumentNullException>(() => new GitHubClient(productInformation, credentialStore, null));
}
[Fact]
public void CreatesAnonymousClientByDefault()
{
var client = new GitHubClient(new ProductHeaderValue("OctokitTests", "1.0"));
Assert.Equal(AuthenticationType.Anonymous, client.Credentials.AuthenticationType);
}
[Fact]
public void CanCreateBasicAuthClient()
{
var client = new GitHubClient(new ProductHeaderValue("OctokitTests", "1.0"))
{
Credentials = new Credentials("tclem", "pwd")
};
Assert.Equal(AuthenticationType.Basic, client.Credentials.AuthenticationType);
}
[Fact]
public void CanCreateOauthClient()
{
var client = new GitHubClient(new ProductHeaderValue("OctokitTests"))
{
Credentials = new Credentials("token")
};
Assert.Equal(AuthenticationType.Oauth, client.Credentials.AuthenticationType);
}
[Theory]
[InlineData("http://github.com", "https://api.github.com/")]
[InlineData("http://github.com/", "https://api.github.com/")]
[InlineData("http://example.com/", "http://example.com/api/v3/")]
[InlineData("http://example.com/anything-really", "http://example.com/api/v3/")]
[InlineData("http://example.com/anything/really/ok", "http://example.com/api/v3/")]
[InlineData("http://example.com/api/v3", "http://example.com/api/v3/")]
[InlineData("https://api.example.com/api/v3", "https://api.example.com/api/v3/")]
public void FixesUpNonGitHubApiAddress(string baseAddress, string expected)
{
var client = new GitHubClient(new ProductHeaderValue("UnitTest"), new Uri(baseAddress));
Assert.Equal(new Uri(expected), client.BaseAddress);
}
}
public class TheBaseAddressProperty
{
[Fact]
public void IsSetToGitHubApiV3()
{
var client = new GitHubClient(new ProductHeaderValue("OctokitTests", "1.0"));
Assert.Equal(new Uri("https://api.github.com"), client.BaseAddress);
}
}
public class TheCredentialsProperty
{
[Fact]
public void DefaultsToAnonymous()
{
var client = new GitHubClient(new ProductHeaderValue("OctokitTests", "1.0"));
Assert.Same(Credentials.Anonymous, client.Credentials);
}
[Fact]
public void WhenSetCreatesInMemoryStoreThatReturnsSpecifiedCredentials()
{
var credentials = new Credentials("Peter", "Griffin");
var client = new GitHubClient(new ProductHeaderValue("OctokitTests"),
Substitute.For<ICredentialStore>())
{
Credentials = credentials
};
Assert.IsType<InMemoryCredentialStore>(client.Connection.CredentialStore);
Assert.Same(credentials, client.Credentials);
}
[Fact]
public void IsRetrievedFromCredentialStore()
{
var credentialStore = Substitute.For<ICredentialStore>();
credentialStore.GetCredentials().Returns(Task.Factory.StartNew(() => new Credentials("foo", "bar")));
var client = new GitHubClient(new ProductHeaderValue("OctokitTests"), credentialStore);
Assert.Equal("foo", client.Credentials.Login);
Assert.Equal("bar", client.Credentials.Password);
}
}
public class TheLastApiInfoProperty
{
[Fact]
public async Task ReturnsNullIfNew()
{
var connection = Substitute.For<IConnection>();
connection.GetLastApiInfo().Returns((ApiInfo)null);
var client = new GitHubClient(connection);
var result = client.GetLastApiInfo();
Assert.Null(result);
var temp = connection.Received(1).GetLastApiInfo();
}
[Fact]
public async Task ReturnsObjectIfNotNew()
{
var apiInfo = new ApiInfo(
new Dictionary<string, Uri>
{
{
"next",
new Uri("https://api.github.com/repos/rails/rails/issues?page=4&per_page=5")
},
{
"last",
new Uri("https://api.github.com/repos/rails/rails/issues?page=131&per_page=5")
},
{
"first",
new Uri("https://api.github.com/repos/rails/rails/issues?page=1&per_page=5")
},
{
"prev",
new Uri("https://api.github.com/repos/rails/rails/issues?page=2&per_page=5")
}
},
new List<string>
{
"user"
},
new List<string>
{
"user",
"public_repo",
"repo",
"gist"
},
"5634b0b187fd2e91e3126a75006cc4fa",
new RateLimit(100, 75, 1372700873)
);
var connection = Substitute.For<IConnection>();
connection.GetLastApiInfo().Returns(apiInfo);
var client = new GitHubClient(connection);
var result = client.GetLastApiInfo();
Assert.NotNull(result);
var temp = connection.Received(1).GetLastApiInfo();
}
}
public class TheSetRequestTimeoutMethod
{
[Fact]
public void SetsTheTimeoutOnTheUnderlyingHttpClient()
{
var httpClient = Substitute.For<IHttpClient>();
var client = new GitHubClient(new Connection(new ProductHeaderValue("OctokitTests"), httpClient));
client.SetRequestTimeout(TimeSpan.FromSeconds(15));
httpClient.Received(1).SetRequestTimeout(TimeSpan.FromSeconds(15));
}
}
}
}