/// <summary> /// Encrypts the data. /// </summary> /// <param name="surveyId">The survey identifier.</param> /// <param name="model">The model.</param> /// <returns></returns> public Task<string> EncryptData(string surveyId, DataCryptographyModel model) { if (string.IsNullOrEmpty((surveyId))) { throw new ArgumentNullException(nameof(surveyId)); } if (model == null) { throw new ArgumentNullException(nameof(model)); } var uri = SurveyDataEncryptionUrl(surveyId); var result = Client.PostAsJsonAsync(uri, model). ContinueWith(task => task.Result.Content.ReadAsAsync<string>().Result) .FlattenExceptions(); return result; }
public void TestEncryption_SurveyId_DoesntExists_ReturnsResourceNotFoundException() { var dataModel = new DataCryptographyModel { Data = new Dictionary<string, string> { { "DataExample1", "ValueExample1" }, { "DataExample2", "ValueExample2" } }, IV = "VGhpc0lzQUJhc2U2NDY0Ng==" }; _mockedNfieldConnection = new Mock<INfieldConnectionClient>(); _mockedHttpClient = CreateHttpClientMock(_mockedNfieldConnection); // API call response var expectedResult = @"IV=VGhpc0lzQUJhc2U2NDY0Ng==&DATA=kDdE+WOvPi45K6q1fC8iLIJ+M7j5xZmETPf24AS81jk="; _mockedHttpClient.Setup(client => client.PostAsJsonAsync($"{ServiceAddress}Surveys/{SurveyId}/RespondentDataEncrypt", dataModel)) .Returns(CreateTask(HttpStatusCode.OK, new ObjectContent<string>(expectedResult, new JsonMediaTypeFormatter()))); _target = new NfieldRespondentDataEncryptService(); _target.InitializeNfieldConnection(_mockedNfieldConnection.Object); var sdkResult = _target.EncryptData(SurveyId, dataModel).Result; Assert.Equal(expectedResult, sdkResult); }