public void Update_without_scopes() { // Arrange var keyId = "xxxxxxxx"; var name = "My API Key"; var scopes = (string[])null; var mockRepository = new MockRepository(MockBehavior.Strict); var mockClient = mockRepository.Create <IClient>(); mockClient .Setup(c => c.PatchAsync($"{ENDPOINT}/{keyId}", It.IsAny <JObject>(), It.IsAny <CancellationToken>())) .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(SINGLE_API_KEY_JSON) }) .Verifiable(); var apiKeys = new ApiKeys(mockClient.Object, ENDPOINT); // Act var result = apiKeys.UpdateAsync(keyId, name, scopes, CancellationToken.None).Result; // Assert result.ShouldNotBeNull(); }
public void Update_without_scopes() { // Arrange var keyId = "xxxxxxxx"; var name = "My API Key"; var scopes = (string[])null; var apiResponse = @"{ 'api_key': 'SG.xxxxxxxx.yyyyyyyy', 'api_key_id': 'xxxxxxxx', 'name': 'My API Key', 'scopes': [ 'mail.send', 'alerts.create', 'alerts.read' ] }" ; var mockClient = new Mock <IClient>(MockBehavior.Strict); mockClient.Setup(c => c.PatchAsync($"{ENDPOINT}/{keyId}", It.IsAny <JObject>(), It.IsAny <CancellationToken>())) .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(apiResponse) }); var apiKeys = new ApiKeys(mockClient.Object); // Act var result = apiKeys.UpdateAsync(keyId, name, scopes, CancellationToken.None).Result; // Assert Assert.IsNotNull(result); }
public async Task <GenerateKeyResponse> GenerateKey(GenerateKeyRequest req, IMemoryCache cache, MySQLDB db) { _cache = cache; Db = db; string APIKey = string.Empty; GenerateKeyResponse response = new GenerateKeyResponse(); using (var cryptoProvider = new RNGCryptoServiceProvider()) { byte[] secretKeyByteArray = new byte[32]; //256 bit cryptoProvider.GetBytes(secretKeyByteArray); APIKey = Convert.ToBase64String(secretKeyByteArray); } string AppID = string.Empty; if (!req.forSession) { AppID = GetMachineGuid(); } else { AppID = Guid.NewGuid().ToString(); } response.AppID = AppID; response.ApiKey = APIKey; if (req.cache) { MemoryCacheEntryOptions cacheExpirationOptions = new MemoryCacheEntryOptions(); cacheExpirationOptions.AbsoluteExpiration = DateTime.Now.AddMinutes(30); cacheExpirationOptions.Priority = CacheItemPriority.High; _cache.Set("AppID", AppID, cacheExpirationOptions); _cache.Set("ApiKey", APIKey, cacheExpirationOptions); } await Db.Connection.OpenAsync(); ApiKeys model = new ApiKeys() { AppID = AppID, ApiKey = APIKey }; model.Db = Db; var query = new ApiKeysQuery(Db); var queryResult = await query.FindOneByIDAsync(AppID); if (queryResult is null) { await model.InsertAsync(); } else { await model.UpdateAsync(); } await Db.Connection.CloseAsync(); return(response); }
public async Task UpdateAsync_without_scopes() { // Arrange var keyId = "xxxxxxxx"; var name = "My API Key"; var scopes = (string[])null; var mockHttp = new MockHttpMessageHandler(); mockHttp.Expect(new HttpMethod("PATCH"), Utils.GetSendGridApiUri(ENDPOINT, keyId)).Respond("application/json", SINGLE_API_KEY_JSON); var client = Utils.GetFluentClient(mockHttp); var apiKeys = new ApiKeys(client); // Act var result = await apiKeys.UpdateAsync(keyId, name, scopes, null, CancellationToken.None).ConfigureAwait(false); // Assert mockHttp.VerifyNoOutstandingExpectation(); mockHttp.VerifyNoOutstandingRequest(); result.ShouldNotBeNull(); }
public void Update_with_scopes() { // Arrange var keyId = "xxxxxxxx"; var name = "My API Key"; var scopes = new[] { "mail.send", "alerts.create", "alerts.read" }; var mockHttp = new MockHttpMessageHandler(); mockHttp.Expect(HttpMethod.Put, Utils.GetSendGridApiUri(ENDPOINT, keyId)).Respond("application/json", SINGLE_API_KEY_JSON); var client = Utils.GetFluentClient(mockHttp); var apiKeys = new ApiKeys(client); // Act var result = apiKeys.UpdateAsync(keyId, name, scopes, CancellationToken.None).Result; // Assert mockHttp.VerifyNoOutstandingExpectation(); mockHttp.VerifyNoOutstandingRequest(); result.ShouldNotBeNull(); }