2using System.Diagnostics;
4using System.Security.Authentication;
13using Microsoft.VisualStudio.TestTools.UnitTesting;
16using TestClass = NUnit.Framework.TestFixtureAttribute;
17using TestMethod = NUnit.Framework.TestAttribute;
18using TestCategory = NUnit.Framework.CategoryAttribute;
26 private const string TestCategory =
"HTTPS API Server";
38 Assert.Inconclusive(
"ARE-939 - non-static API");
47 #region Test Valid Credentials
48 [TestMethod, TestCategory(TestCategory)]
56 [TestMethod, TestCategory(TestCategory)]
59 API.AuthenticationRequested += ValidAuthenticationRequestedHandler;
64 [TestMethod, TestCategory(TestCategory)]
67 API.AuthenticationRequested += (ref
IAccessToken authenticate,
string auth) =>
68 Console.WriteLine(
"I'm just an AuthenticationRequested logger.");
69 API.AuthenticationRequested += ValidAuthenticationRequestedHandler;
70 API.AuthenticationRequested += (ref
IAccessToken authenticate,
string auth) =>
71 Console.WriteLine(
"I'm just an AuthenticationRequested logger too.");
75 #endregion Test Valid Credentials
77 #region Test Invalid AuthenticationToken Credentials
78 [TestMethod, TestCategory(TestCategory)]
81 API.AuthenticationToken =
null;
83 "settings have been configured. Please either handle the " +
84 "API.AuthenticationRequested event appropriately, or set " +
85 "the global default AnalyzeRe.API.AuthenticationToken directly.");
88 [TestMethod, TestCategory(TestCategory)]
94 "authentication token " +
@"(Basic YmFkOmJhZA==)" +
95 " was rejected. Please verify the credentials supplied.");
99 [TestMethod, TestCategory(TestCategory)]
105 "authentication token " +
@"(Basic )" +
106 " was rejected. Please verify the credentials supplied.");
110 [TestMethod, TestCategory(TestCategory)]
116 "authentication token " +
@"(Basic Og==)" +
117 " was rejected. Please verify the credentials supplied.");
120 #endregion Test Invalid AuthenticationToken Credentials
122 #region Test Invalid AuthenticationRequested Event
128 [TestMethod, TestCategory(TestCategory)]
131 API.AuthenticationRequested += (ref
IAccessToken authenticate,
string auth) =>
134 " failed authentication attempts.");
138 [TestMethod, TestCategory(TestCategory)]
142 "settings have been configured. Please either handle the " +
143 "API.AuthenticationRequested event appropriately, or set " +
144 "the global default AnalyzeRe.API.AuthenticationToken directly.");
148 [TestMethod, TestCategory(TestCategory)]
151 API.AuthenticationRequested += (ref
IAccessToken authenticate,
string auth) => { };
156 [TestMethod, TestCategory(TestCategory)]
159 API.AuthenticationRequested += (ref
IAccessToken authenticate,
string auth) =>
160 {
throw new Exception(
"I don't know what I'm doing"); };
162 "I don't know what I'm doing");
165 #endregion Test Invalid AuthenticationRequested Event
167 #region Test GetAuthenticationStatus
168 [TestMethod, TestCategory(TestCategory)]
171 API.AuthenticationToken =
null;
175 [TestMethod, TestCategory(TestCategory)]
183 [TestMethod, TestCategory(TestCategory), TestCategory(
"Skipped")]
189 "cannot test the response when a server doesn't require authentication.");
190 API.ServerURL = _httpsUrl;
194 [TestMethod, TestCategory(TestCategory)]
202 [TestMethod, TestCategory(TestCategory)]
205 API.ServerURL =
"Invalid";
208 #endregion Test GetAuthenticationStatus
210 #region Test Credential Caching
212 [TestMethod, TestCategory(TestCategory)]
215 API.AuthenticationRequested += ValidAuthenticationRequestedHandler;
217 API.AuthenticationRequested -= ValidAuthenticationRequestedHandler;
220 API.ServerURL =
null;
223 API.ServerURL = _httpsUrl;
231 [TestMethod, TestCategory(TestCategory)]
234 API.AuthenticationRequested += ValidAuthenticationRequestedHandler;
236 API.AuthenticationRequested -= ValidAuthenticationRequestedHandler;
239 API.ServerURL =
null;
244 API.ServerURL = _httpsUrl;
247 "settings have been configured. Please either handle the " +
248 "API.AuthenticationRequested event appropriately, or set " +
249 "the global default AnalyzeRe.API.AuthenticationToken directly.");
253 #endregion Test Credential Caching
255 #region Miscellaneous Tests
258 [TestMethod, TestCategory(TestCategory)]
262 "No active server found at " + _httpsUrl);
266 [TestMethod, TestCategory(TestCategory)]
270 API.AuthenticationRequested += ValidAuthenticationRequestedHandler;
275 API.AuthenticationToken =
null;
276 void StreamReader(Stream stream) =>
277 Console.WriteLine(
"Stream contained:\n" +
new StreamReader(stream).ReadToEnd());
278 void TestGetStream() => eventCatalog.
data.
GetStream(StreamReader);
281 "The initial request could not be completed due to an authentication error. " +
282 "Please reset the stream and try again."
294 #endregion Miscellaneous Tests
296 #region Test Override Authentication
299 [TestMethod, TestCategory(TestCategory)]
304 Name =
"Authorization",
306 Type = ParameterType.HttpHeader
314 [TestMethod, TestCategory(TestCategory)]
317 AssertApi.ExceptionThrown<AuthenticationException>(() =>
320 Name =
"Authorization",
322 Type = ParameterType.HttpHeader
324 ex => Assert.AreEqual(
325 "The current request included an authentication token (bad) " +
326 "which was rejected by the server. Either correct the \"Authorization\" " +
327 "header parameter supplied to this request, or exclude it entirely and use " +
328 "the 'API.AuthenticationRequested' event or 'API.AuthenticationToken' to " +
329 "automatically handle authentication for Analyze Re API requests."
335 [TestMethod, TestCategory(TestCategory)]
339 API.AuthenticationToken = token;
340 AssertApi.ExceptionThrown<AuthenticationException>(() =>
343 Name =
"Authorization",
345 Type = ParameterType.HttpHeader
347 ex => Assert.AreEqual(
348 "The current request included an authentication token (Basic YmFkOmJhZA==) " +
349 "which was rejected by the server. Either correct the \"Authorization\" " +
350 "header parameter supplied to this request, or exclude it entirely and use " +
351 "the 'API.AuthenticationRequested' event or 'API.AuthenticationToken' to " +
352 "automatically handle authentication for Analyze Re API requests."
359 [TestMethod, TestCategory(TestCategory)]
362 API.AuthenticationToken = GetCorrectCredentials();
363 AssertApi.ExceptionThrown<AuthenticationException>(() =>
366 Name =
"Authorization",
368 Type = ParameterType.HttpHeader
370 ex => Assert.AreEqual(
371 "The current request included an authentication token (bad) which was rejected " +
372 "by the server, and which differs from the currently configured default " +
373 "authentication token (" + GetCorrectCredentials().ToString() +
"). Either " +
374 "correct the \"Authorization\" header parameter supplied to this request, " +
375 "or exclude it entirely and the default authentication token will be used."
378 #endregion Test Override Authentication
380 #region Helper Methods
381 private void ValidAuthenticationRequestedHandler(ref
IAccessToken authenticate,
string auth)
401 AssertApi.ExceptionThrown<AuthenticationException>(
404 Assert.AreEqual(expectedMessage, ex.Message);
405 Debug.WriteLine(
"Caught expected exception: " + ex);
408 #endregion Helper Methods
void AddCommonTestCleanupAction(Action action)
void AddCommonTestInitializeAction(Action action)
Exposes sample resource objects, with built-in methods for injecting dependencies.
IInjectableResource< EventCatalog > EventCatalog
void Test_API_HTTPS_Server_Credentials_Cached()
Note: Credential caching only works using the AuthenticationRequested event.
void Test_API_HTTPS_Server_Active()
Ensure that the server shows up as an active analyze re server, even without credentials.
void Test_API_HTTPS_Server_BasicAuthenticationToken_Empty()
IAccessToken GetCorrectCredentials()
void Test_API_HTTPS_Server_BasicAuthenticationToken_Invalid()
void Test_API_HTTPS_Server_GetAuthenticationStatus_Authenticated()
void Test_API_HTTPS_Server_AuthenticationRequestedEvent_MultipleHandlers()
void Test_API_HTTPS_Server_OverrideAuthentication_Invalid_MatchingDefaultAuth()
Test that overriding the default authentication credentials with the same bad parameters results in a...
void Test_API_HTTPS_Server_AuthenticationRequestedEvent_Invalid()
Test an authentication handler incorrectly set up to supply the same invalid credentials on every cal...
void Test_API_HTTPS_Server_AuthenticationRequestedEvent_Exception()
void Test_API_HTTPS_Server_AuthenticationRequestedEvent_GetStream()
Test that an authentication error on GetStream expected behaviour.
void Test_API_HTTPS_Server_OverrideAuthentication_Valid()
Test that the user can supply credentials without setting the global default authentication settings.
void Test_API_HTTPS_Server_AuthenticationRequestedEvent_HandledEmpty()
void Test_API_HTTPS_Server_GetAuthenticationStatus_AuthenticationRequired()
void Test_API_HTTPS_Server_GetAuthenticationStatus_Error()
void TestUnauthorizedRequest(string expectedMessage)
void Test_API_HTTPS_Server_GetAuthenticationStatus_NoAuthenticationRequired()
void Test_API_HTTPS_Server_AuthenticationRequestedEvent_NotHandled()
void Test_API_HTTPS_Server_BasicAuthenticationToken_Valid()
void Test_API_HTTPS_Server_OverrideAuthentication_Invalid_NoDefaultAuth()
Test that manually specifying incorrect authentication credentials when there are no default credenti...
void TestSuccessfulRequest()
void Test_API_HTTPS_Server_AuthenticationRequestedEvent_ValidAccessToken()
void Test_API_HTTPS_Server_CachedCredentialsCleared()
Note: Credential caching only works using the AuthenticationRequested event.
void Test_API_HTTPS_Server_GetAuthenticationStatus_InvalidCredentials()
void Test_API_HTTPS_Server_OverrideAuthentication_Invalid_NotMatchingDefaultAuth()
Test that overriding the current default authentication credentials (which are good) with some bad pa...
void Test_API_HTTPS_Server_BasicAuthenticationToken_Null()
void Test_API_HTTPS_Server_BasicAuthenticationToken_NullUserNamePassword()
Retrieve settings from environment variables if they exist, or the project settings file otherwise.
static string API_URL
The URL against which server tests are run.
static string HTTPS_USER
The username for the HTTPS server authentication.
static string HTTPS_PASSWORD
The password for the HTTPS server authentication.
Class used in unit tests to mark tests as skipped by using Assert.Inconclusive() method.
static void Indefinitely(string ticket=null)
Skip the specified test.
A custom exception class that includes the RestSharp.IRestResponse that generated the exception,...
LargeDataSubResource data
An endpoint for accessing or modifying the data file attached to this resource. Also contains a Large...
IRestResponse GetStream(Action< Stream > consumeStream, IEnumerable< Parameter > parameters=null, int? timeout=null)
Perform a GET on the supplied URL and provide an action for consuming the response stream rather than...
API methods / requests made available to the user.
static IAccessToken AuthenticationToken
The AccessToken storing authentication information for requests made to the server.
static volatile int MaxRequestRetries
When a temporary communication failure occurs (such as a socket error or an authentication failure fo...
static bool IsAReServerActive(string checkServerURL, int? timeout=null)
Determines if the provided URL is a valid running Analyze Re server.
static ICollectionResponse< IAPIResource > GetResourceList(Type resourceType, IEnumerable< Parameter > requestParameters=null, string collectionNameOverride=null, int? timeout=null)
Get a collection of resources from the server.
static AuthenticationStatus GetAuthenticationStatus(bool force_request=false)
Determines whether we are currently authenticated against the server with the global default Authenti...
static void ClearCachedAuthenticationCredentials(string server=null)
The Analyze Re API .NET Client Library caches authentication tokens that have been validated via the ...
A configuration of resources used to simulate a layer or portfolio.
An AccessToken storing basic authentication information for requests made to the server.
override string ToString()
Returns the representation of this access token that could be placed in an "Authorization" header.
Representation of an event catalog. The event catalog may cover multiple region/perils,...
T Posted
The posted resource, ready to be referenced.
An AccessToken storing authentication information for requests made to the server.
AccessTokenStatus status
The AccessToken's current AccessTokenStatus (unauthorized / valid).
AccessTokenStatus
An AccessToken's status (unauthorized / valid).
AuthenticationStatus
The status of authentication against the current server.