public async Task WhenSubjectAndKeyContainRestrictedCharacters_GetIndividualBySubject_UrlEncodesRestrictedCharacters() { // Setup mock expectation for a encoded url call var mockHttpHandler = new MockHttpMessageHandler(); var response = @"{ 'id': 'ca98dfajl3kja9kjh34589734', 'applicationId': 'ci123doiu3', 'key': 'testKey 123', 'subject': 'test:subject=123', 'value': 5 }" .Replace("'", "\""); mockHttpHandler.Expect(HttpMethod.Get, "https://api.useast1.badgeup.io/v2/apps/1337/metrics/test%3Asubject%3D123/testKey%20123").Respond("application/json", response); using (var badgeUpClient = new BadgeUpHttpClient(ApiKey.Create("eyJhY2NvdW50SWQiOiJ0aGViZXN0IiwiYXBwbGljYXRpb25JZCI6IjEzMzciLCJrZXkiOiJpY2VjcmVhbWFuZGNvb2tpZXN5dW0ifQ=="), "https://api.useast1.badgeup.io")) { badgeUpClient.SetHttpClient(mockHttpHandler.ToHttpClient()); // pass subject and key with restricted characters var metricClient = new MetricClient(badgeUpClient); var metric = await metricClient.GetIndividualBySubject("test:subject=123", "testKey 123"); Assert.Equal("test:subject=123", metric.Subject); Assert.Equal("testKey 123", metric.Key); } // expect encoded url to have been called mockHttpHandler.VerifyNoOutstandingExpectation(); }
public async void HttpClient_Post_Retry_success_with_two_errors() { // setup the responses var errorResponse = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent("{content}") }; var successResponse = new HttpResponseMessage(HttpStatusCode.Created) { Content = new StringContent("{foo: \"bar\"}") }; //setup mock httpClient var mockHttp = new MockHttpMessageHandler(); mockHttp.Expect(HttpMethod.Post, "http://www.x.badgeup.io/v2/apps/1337/testEndpoint").Respond(r => errorResponse); mockHttp.Expect(HttpMethod.Post, "http://www.x.badgeup.io/v2/apps/1337/testEndpoint").Throw(new TaskCanceledException("Simulated timeout exception")); mockHttp.Expect(HttpMethod.Post, "http://www.x.badgeup.io/v2/apps/1337/testEndpoint").Respond(r => successResponse); //Test var client = new BadgeUpHttpClient(ApiKey.Create(Key), "http://www.x.badgeup.io"); client.SetHttpClient(mockHttp.ToHttpClient()); await client.Post <object>(new EventRequest(new Event("subject", "key")), "testEndpoint"); mockHttp.VerifyNoOutstandingExpectation(); }
public async Task WhenSubjectAndKeyContainRestrictedCharacters_GetAllBySubject_UrlEncodesRestrictedCharacters() { // Setup mock expectation for a encoded url call var mockHttpHandler = new MockHttpMessageHandler(); var response = @"{ 'pages': { 'previous': null, 'next': null }, 'data': [ { 'id': 'cja8a0980j3jkajhaaa345810', 'applicationId': 'e7kmr6m0ob', 'key': 'test', 'subject': 'test:subject=123', 'value': 5 }, { 'id': 'cjmx29083hjkaaa325usgaitx', 'applicationId': 'e7kmr6m0ob', 'key': 'test:key', 'subject': 'test:subject=123', 'value': 1 }, { 'id': 'cjmy215tq87hdo1b9hrb2qlsj', 'applicationId': 'e7kmr6m0ob', 'key': 'test:metric', 'subject': 'test:subject=123', 'value': 5 } ] }" .Replace("'", "\""); mockHttpHandler.Expect(HttpMethod.Get, "https://api.useast1.badgeup.io/v2/apps/1337/metrics/test%3Asubject%3D123").Respond("application/json", response); using (var badgeUpClient = new BadgeUpHttpClient(ApiKey.Create("eyJhY2NvdW50SWQiOiJ0aGViZXN0IiwiYXBwbGljYXRpb25JZCI6IjEzMzciLCJrZXkiOiJpY2VjcmVhbWFuZGNvb2tpZXN5dW0ifQ=="), "https://api.useast1.badgeup.io")) { badgeUpClient.SetHttpClient(mockHttpHandler.ToHttpClient()); // pass subject and key with restricted characters var metricClient = new MetricClient(badgeUpClient); var metrics = await metricClient.GetAllBySubject("test:subject=123"); Assert.Equal(3, metrics.Count); Assert.Equal("test:subject=123", metrics[0].Subject); Assert.Equal("test:subject=123", metrics[1].Subject); Assert.Equal("test:subject=123", metrics[2].Subject); Assert.Equal("test", metrics[0].Key); Assert.Equal("test:key", metrics[1].Key); Assert.Equal("test:metric", metrics[2].Key); } // expect encoded url to have been called mockHttpHandler.VerifyNoOutstandingExpectation(); }
public async Task WhenShowIncompleteIsFalse_SendV2Preview_MustNotAppendShowIncompleteQueryParam() { using (var badgeUpClient = new BadgeUpHttpClient(ApiKey.Create("eyJhY2NvdW50SWQiOiJ0aGViZXN0IiwiYXBwbGljYXRpb25JZCI6IjEzMzciLCJrZXkiOiJpY2VjcmVhbWFuZGNvb2tpZXN5dW0ifQ=="), "https://api.useast1.badgeup.io")) { // Setup mock response // When there's a showIncomplete=false query param, throw an exception. var mockHttpHandler = new MockHttpMessageHandler(); mockHttpHandler.When(HttpMethod.Post, "https://api.useast1.badgeup.io/v2/apps/1337/events*") .WithQueryString("showIncomplete", "false") .Throw(new InvalidOperationException("URL shouldn't have the showIncomplete param appended")); mockHttpHandler.Fallback.Respond("application/json", @"{ 'event': { 'id': 'cja92jvpj1gummf5lf3jj5fx3', 'applicationId': '1337', 'subject': 'subject-1', 'key': 'jump', 'timestamp': '2017-11-21T03:37:03.559Z', 'modifier': { '@inc': 1 } }, 'progress': [ { 'achievementId': 'cj1sp5nse02j9zkruwhb3zwik', 'earnedAchievementId': 'cj1ss153y02k1zkrun39g8itq', 'isComplete': true, 'isNew': true, 'percentComplete': 1, 'progressTree': { 'type': 'GROUP', 'groups': [], 'criteria': { 'cj1sp461o02imzkruqkqi8amh': 1 }, 'condition': 'AND' } } ] }" .Replace("'", "\"")); // arrange badgeUpClient.SetHttpClient(mockHttpHandler.ToHttpClient()); var client = new EventClient(badgeUpClient); // act await client.Send(new BadgeUp.Types.Event("subject-1", "jump"), showIncomplete : false); await client.Send(new BadgeUp.Types.Event("subject-1", "jump"), showIncomplete : true); await client.Send(new BadgeUp.Types.Event("subject-1", "jump"), showIncomplete : null); } }
public async void BasicIntegration_GetAccount() { if (string.IsNullOrEmpty(API_KEY)) { throw new SkipException("Tests skipped on environments without API_KEY variable configured"); } var client = new BadgeUpClient(API_KEY); var apiKey = ApiKey.Create(API_KEY); var account = await client.Account.GetById(apiKey.AccountId); Assert.Equal(apiKey.AccountId, account.Id); }
public async void BasicIntegration_GetIcons() { if (string.IsNullOrEmpty(API_KEY)) { throw new SkipException("Tests skipped on environments without API_KEY variable configured"); } var client = new BadgeUpClient(API_KEY); var apiKey = ApiKey.Create(API_KEY); //some achievement icons have to be uploaded for the test to pass. var icons = await client.AchievementIcon.GetAll(); Assert.False(icons.Length == 0); }
public async void HttpClient_Get_Retry_fails_after_three_errors() { // setup the responses var errorResponse = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent("{content}") }; //setup mock httpClient var mockHttp = new MockHttpMessageHandler(); mockHttp.Expect(HttpMethod.Get, "http://www.x.badgeup.io/v2/apps/1337/").Respond(r => errorResponse); mockHttp.Expect(HttpMethod.Get, "http://www.x.badgeup.io/v2/apps/1337/").Throw(new TaskCanceledException("Simulated timeout exception")); mockHttp.Expect(HttpMethod.Get, "http://www.x.badgeup.io/v2/apps/1337/").Respond(r => errorResponse); //Test var client = new BadgeUpHttpClient(ApiKey.Create(Key), "http://www.x.badgeup.io"); client.SetHttpClient(mockHttp.ToHttpClient()); await Assert.ThrowsAsync <BadgeUpClientException>(async() => await client.Get <object>("")); mockHttp.VerifyNoOutstandingExpectation(); }
public void ApiKey_Key() { var apiKey = ApiKey.Create(key); Assert.Equal("icecreamandcookiesyum", apiKey.Key); }
public void ApiKey_AccountId() { var apiKey = ApiKey.Create(key); Assert.Equal("thebest", apiKey.AccountId); }
public void ApiKey_ApplicationId() { var apiKey = ApiKey.Create(key); Assert.Equal("1337", apiKey.ApplicationId); }
public void CreateObjectFromOwner(ApiKey _ApiKey, Services.Packages.Security.ModelUser _Owner, string SessionToken) { ModelSession session = ApiKeySecureService.CheckSessionImp(SessionToken); //if (!Services.Packages.Security.Security.ModelUserSecureService.CheckAccessImp(_Owner, SessionToken, "Create ApiKeyOwning")) // throw new UnauthorizedAccessException("Access Denied"); _ApiKey.Reload(); if (!_ApiKey.Exists && !Services.Packages.Security.Security.ModelUserSecureService.CheckAccessImp(_Owner, SessionToken, "Create ApiKeyOwning")) throw new UnauthorizedAccessException("Access Denied"); else if (_ApiKey.Exists && !ApiKeySecureService.CheckAccessImp(_ApiKey, SessionToken, "Update")) throw new UnauthorizedAccessException("Access Denied"); _ApiKey.Owner = session.User; _ApiKey.Owner = _Owner; _ApiKey.Create(); }
public void CreateObject(ApiKey _ApiKey, string SessionToken) { ModelSession session = ApiKeySecureService.CheckSessionImp(SessionToken); if (!ApiKeySecureService.CheckUserRightsImp(session.User.Name, "Create " + typeof(ApiKey).FullName)) throw new UnauthorizedAccessException("Access Denied"); _ApiKey.Owner = session.User; _ApiKey.Create(); }