Skip to content

Commit

Permalink
Merge pull request #28 from FrendsPlatform/issue26
Browse files Browse the repository at this point in the history
Salesforce ExecuteQuery - CreateSObject returns a null ErrorMessage on error
  • Loading branch information
jefim authored Aug 16, 2024
2 parents 721f315 + 9ff4ff3 commit ae5b69c
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/CreateSObject_build_and_test_on_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ jobs:
env_var_name_2: Salesforce_Password
env_var_name_3: Salesforce_Security_Token
env_var_name_4: Salesforce_ClientID
env_var_name_5: Salesforce_Username
secrets:
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
env_var_value_1: ${{ secrets.SALESFORCE_CLIENT_SECRET }}
env_var_value_2: ${{ secrets.SALESFORCE_PASSWORD }}
env_var_value_3: ${{ secrets.SALESFORCE_SECURITY_TOKEN }}
env_var_value_4: ${{ secrets.SALESFORCE_CLIENTID }}
env_var_value_5: ${{ secrets.SALESFORCE_USERNAME }}
2 changes: 2 additions & 0 deletions .github/workflows/CreateSObject_build_and_test_on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ jobs:
env_var_name_2: Salesforce_Password
env_var_name_3: Salesforce_Security_Token
env_var_name_4: Salesforce_ClientID
env_var_name_5: Salesforce_Username
secrets:
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
test_feed_api_key: ${{ secrets.TASKS_TEST_FEED_API_KEY }}
env_var_value_1: ${{ secrets.SALESFORCE_CLIENT_SECRET }}
env_var_value_2: ${{ secrets.SALESFORCE_PASSWORD }}
env_var_value_3: ${{ secrets.SALESFORCE_SECURITY_TOKEN }}
env_var_value_4: ${{ secrets.SALESFORCE_CLIENTID }}
env_var_value_5: ${{ secrets.SALESFORCE_USERNAME }}
2 changes: 2 additions & 0 deletions .github/workflows/DeleteSObject_build_and_test_on_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ jobs:
env_var_name_2: Salesforce_Password
env_var_name_3: Salesforce_Security_Token
env_var_name_4: Salesforce_ClientID
env_var_name_5: Salesforce_Username
secrets:
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
env_var_value_1: ${{ secrets.SALESFORCE_CLIENT_SECRET }}
env_var_value_2: ${{ secrets.SALESFORCE_PASSWORD }}
env_var_value_3: ${{ secrets.SALESFORCE_SECURITY_TOKEN }}
env_var_value_4: ${{ secrets.SALESFORCE_CLIENTID }}
env_var_value_5: ${{ secrets.SALESFORCE_USERNAME }}
2 changes: 2 additions & 0 deletions .github/workflows/DeleteSObject_build_and_test_on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ jobs:
env_var_name_2: Salesforce_Password
env_var_name_3: Salesforce_Security_Token
env_var_name_4: Salesforce_ClientID
env_var_name_5: Salesforce_Username
secrets:
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
test_feed_api_key: ${{ secrets.TASKS_TEST_FEED_API_KEY }}
env_var_value_1: ${{ secrets.SALESFORCE_CLIENT_SECRET }}
env_var_value_2: ${{ secrets.SALESFORCE_PASSWORD }}
env_var_value_3: ${{ secrets.SALESFORCE_SECURITY_TOKEN }}
env_var_value_4: ${{ secrets.SALESFORCE_CLIENTID }}
env_var_value_5: ${{ secrets.SALESFORCE_USERNAME }}
2 changes: 2 additions & 0 deletions .github/workflows/ExecuteQuery_build_and_test_on_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ jobs:
env_var_name_2: Salesforce_Password
env_var_name_3: Salesforce_Security_Token
env_var_name_4: Salesforce_ClientID
env_var_name_5: Salesforce_Username
secrets:
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
env_var_value_1: ${{ secrets.SALESFORCE_CLIENT_SECRET }}
env_var_value_2: ${{ secrets.SALESFORCE_PASSWORD }}
env_var_value_3: ${{ secrets.SALESFORCE_SECURITY_TOKEN }}
env_var_value_4: ${{ secrets.SALESFORCE_CLIENTID }}
env_var_value_5: ${{ secrets.SALESFORCE_USERNAME }}
2 changes: 2 additions & 0 deletions .github/workflows/ExecuteQuery_build_and_test_on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ jobs:
env_var_name_2: Salesforce_Password
env_var_name_3: Salesforce_Security_Token
env_var_name_4: Salesforce_ClientID
env_var_name_5: Salesforce_Username
secrets:
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
test_feed_api_key: ${{ secrets.TASKS_TEST_FEED_API_KEY }}
env_var_value_1: ${{ secrets.SALESFORCE_CLIENT_SECRET }}
env_var_value_2: ${{ secrets.SALESFORCE_PASSWORD }}
env_var_value_3: ${{ secrets.SALESFORCE_SECURITY_TOKEN }}
env_var_value_4: ${{ secrets.SALESFORCE_CLIENTID }}
env_var_value_5: ${{ secrets.SALESFORCE_USERNAME }}
6 changes: 4 additions & 2 deletions .github/workflows/UpdateSObject_build_and_test_on_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name: UpdateSObject_build_main

on:
push:
branches:
branches:
- main
paths:
- 'Frends.Salesforce.UpdateSObject/**'
- "Frends.Salesforce.UpdateSObject/**"
workflow_dispatch:

jobs:
Expand All @@ -17,9 +17,11 @@ jobs:
env_var_name_2: Salesforce_Password
env_var_name_3: Salesforce_Security_Token
env_var_name_4: Salesforce_ClientID
env_var_name_5: Salesforce_Username
secrets:
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
env_var_value_1: ${{ secrets.SALESFORCE_CLIENT_SECRET }}
env_var_value_2: ${{ secrets.SALESFORCE_PASSWORD }}
env_var_value_3: ${{ secrets.SALESFORCE_SECURITY_TOKEN }}
env_var_value_4: ${{ secrets.SALESFORCE_CLIENTID }}
env_var_value_5: ${{ secrets.SALESFORCE_USERNAME}}
2 changes: 2 additions & 0 deletions .github/workflows/UpdateSObject_build_and_test_on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ jobs:
env_var_name_2: Salesforce_Password
env_var_name_3: Salesforce_Security_Token
env_var_name_4: Salesforce_ClientID
env_var_name_5: Salesforce_Username
secrets:
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
test_feed_api_key: ${{ secrets.TASKS_TEST_FEED_API_KEY }}
env_var_value_1: ${{ secrets.SALESFORCE_CLIENT_SECRET }}
env_var_value_2: ${{ secrets.SALESFORCE_PASSWORD }}
env_var_value_3: ${{ secrets.SALESFORCE_SECURITY_TOKEN }}
env_var_value_4: ${{ secrets.SALESFORCE_CLIENTID }}
env_var_value_5: ${{ secrets.SALESFORCE_USERNAME}}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,6 @@ ModelManifest.xml

# FAKE - F# Make
.fake/

# Dev env files
.env*
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="dotenv.net" Version="3.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
<PackageReference Include="nunit" Version="3.12.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
Expand All @@ -7,6 +8,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using RestSharp;
using dotenv.net;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace Frends.Salesforce.CreateSObject.Tests;
Expand All @@ -17,9 +19,9 @@ public class UnitTests
private readonly string _password = Environment.GetEnvironmentVariable("Salesforce_Password");
private readonly string _securityToken = Environment.GetEnvironmentVariable("Salesforce_Security_Token");
private readonly string _clientID = Environment.GetEnvironmentVariable("Salesforce_ClientID");
private readonly string _username = Environment.GetEnvironmentVariable("Salesforce_Username");

private readonly string _domain = @"https://hiqfinlandoy2-dev-ed.my.salesforce.com";
private readonly string _username = "testuser@test.fi";
private readonly string _domain = @"https://frends2-dev-ed.develop.my.salesforce.com";
private readonly string _authurl = @"https://login.salesforce.com/services/oauth2/token";

private readonly CancellationToken _cancellationToken = new();
Expand All @@ -29,6 +31,17 @@ public class UnitTests

private string _name = "Test" + DateTime.Now.Year + "" + DateTime.Now.Month + "" + DateTime.Now.Day + "" + DateTime.Now.Hour + "" + DateTime.Now.Minute + "" + DateTime.Now.Millisecond;

[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
// load envs
var root = Directory.GetCurrentDirectory();
var projDir = Directory.GetParent(root)?.Parent?.Parent?.FullName;
DotEnv.Load(
options: new DotEnvOptions(
envFilePaths: new[] { $"{projDir}{Path.DirectorySeparatorChar}.env.local" }));
}

[TestInitialize]
public async Task TestInitialize()
{
Expand All @@ -48,7 +61,8 @@ public async Task TestCleanUp()
{
if (_result != null)
{
for (var i = (_result.Count-1); i >= 0; i--) {
for (var i = (_result.Count - 1); i >= 0; i--)
{
var temp = JsonConvert.SerializeObject(_result[i]);
var obj = JsonConvert.DeserializeObject<dynamic>(temp);

Expand Down Expand Up @@ -81,7 +95,8 @@ public async Task CreateAccountTest()
}

[TestMethod]
public async Task CreateContactTest() {
public async Task CreateContactTest()
{
var json = JsonSerializer.Serialize(
new
{
Expand Down Expand Up @@ -122,7 +137,8 @@ public async Task CreateCaseTest()
_result.Add(new { Type = "Account", Id = accObj.id });

// Creating a case.
var json = JsonSerializer.Serialize(new {
var json = JsonSerializer.Serialize(new
{
AccountId = accObj.id.ToString(),
Subject = "This is a test.",
Description = "This is a test case for Frends.SalesForce.CreateSObject task.",
Expand Down Expand Up @@ -174,7 +190,8 @@ public async Task GetReturnedAccessTokenTest()

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public async Task EmptyAccessToken_ThrowTest() {
public async Task EmptyAccessToken_ThrowTest()
{
var input = new Input
{
Domain = _domain,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ CancellationToken cancellationToken
var request = new RestRequest("/", Method.Post);
string accessToken = "";

switch (options.AuthenticationMethod) {
switch (options.AuthenticationMethod)
{
case AuthenticationMethod.AccessToken:
if (string.IsNullOrWhiteSpace(options.AccessToken)) throw new ArgumentException("Access token cannot be null when using Access Token authentication method");
request.AddHeader("Authorization", "Bearer " + options.AccessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="dotenv.net" Version="3.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
<PackageReference Include="nunit" Version="3.12.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Frends.Salesforce.DeleteSObject.Definitions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using dotenv.net;
using RestSharp;
using JsonSerializer = System.Text.Json.JsonSerializer;

Expand All @@ -18,9 +20,9 @@ public class UnitTests
private readonly string _password = Environment.GetEnvironmentVariable("Salesforce_Password");
private readonly string _securityToken = Environment.GetEnvironmentVariable("Salesforce_Security_Token");
private readonly string _clientID = Environment.GetEnvironmentVariable("Salesforce_ClientID");
private readonly string _username = Environment.GetEnvironmentVariable("Salesforce_Username");

private readonly string _domain = @"https://hiqfinlandoy2-dev-ed.my.salesforce.com";
private readonly string _username = "testuser@test.fi";
private readonly string _domain = @"https://frends2-dev-ed.develop.my.salesforce.com";
private readonly string _authurl = @"https://login.salesforce.com/services/oauth2/token";

private readonly CancellationToken _cancellationToken = new();
Expand All @@ -29,6 +31,17 @@ public class UnitTests

readonly string _name = "Test" + DateTime.Now.Year + "" + DateTime.Now.Month + "" + DateTime.Now.Day + "" + DateTime.Now.Hour + "" + DateTime.Now.Minute + "" + DateTime.Now.Millisecond;

[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
// load envs
var root = Directory.GetCurrentDirectory();
var projDir = Directory.GetParent(root)?.Parent?.Parent?.FullName;
DotEnv.Load(
options: new DotEnvOptions(
envFilePaths: new[] { $"{projDir}{Path.DirectorySeparatorChar}.env.local" }));
}

[TestInitialize]
public async Task TestInitialize()
{
Expand All @@ -51,7 +64,8 @@ public async Task DeleteAccountTest()
}

[TestMethod]
public async Task DeleteContactTest() {
public async Task DeleteContactTest()
{
var json = JsonSerializer.Serialize(
new
{
Expand All @@ -72,7 +86,8 @@ public async Task DeleteCaseTest()
var accountId = await CreateSObject("Account", _userJson);

// Creating a case.
var json = JsonSerializer.Serialize(new {
var json = JsonSerializer.Serialize(new
{
AccountId = accountId,
Subject = "This is a test.",
Description = "This is a test case for Frends.SalesForce.CreateSObject task.",
Expand Down Expand Up @@ -109,7 +124,8 @@ public async Task GetReturnedAccessTokenTest()

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public async Task EmptyAccessToken_ThrowTest() {
public async Task EmptyAccessToken_ThrowTest()
{
var input = new Input
{
Domain = _domain,
Expand Down Expand Up @@ -299,8 +315,8 @@ public async Task NotFoundId_ThrowTest()
await Salesforce.DeleteSObject(input, options, _cancellationToken);
}

// Helper method to create SObjects for delete function.
private async Task<string> CreateSObject(string type, string input)
// Helper method to create SObjects for delete function.
private async Task<string> CreateSObject(string type, string input)
{
var client = new RestClient(_domain + "/services/data/v54.0/sobjects/" + type);
var request = new RestRequest("/", Method.Post);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ CancellationToken cancellationToken
var request = new RestRequest("/", Method.Delete);
string accessToken = "";

switch (options.AuthenticationMethod) {
switch (options.AuthenticationMethod)
{
case AuthenticationMethod.AccessToken:
if (string.IsNullOrWhiteSpace(options.AccessToken)) throw new ArgumentException("Access token cannot be null when using Access Token authentication method");
request.AddHeader("Authorization", "Bearer " + options.AccessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="dotenv.net" Version="3.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
<PackageReference Include="nunit" Version="3.12.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Frends.Salesforce.ExecuteQuery.Definitions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using dotenv.net;
using NUnit.Framework.Internal;

namespace Frends.Salesforce.ExecuteQuery.Tests;
[TestClass]
Expand All @@ -12,12 +15,22 @@ public class UnitTests
private readonly string _password = Environment.GetEnvironmentVariable("Salesforce_Password");
private readonly string _securityToken = Environment.GetEnvironmentVariable("Salesforce_Security_Token");
private readonly string _clientID = Environment.GetEnvironmentVariable("Salesforce_ClientID");

private readonly string _domain = @"https://hiqfinlandoy2-dev-ed.my.salesforce.com";
private readonly string _username = "testuser@test.fi";
private readonly string _username = Environment.GetEnvironmentVariable("Salesforce_Username");
private readonly string _domain = @"https://frends2-dev-ed.develop.my.salesforce.com";
private readonly string _authurl = @"https://login.salesforce.com/services/oauth2/token";
private readonly CancellationToken _cancellationToken = new();

[ClassInitialize]
public static void TestInitialize(TestContext testContext)
{
// load envs
var root = Directory.GetCurrentDirectory();
var projDir = Directory.GetParent(root)?.Parent?.Parent?.FullName;
DotEnv.Load(
options: new DotEnvOptions(
envFilePaths: new[] { $"{projDir}{Path.DirectorySeparatorChar}.env.local" }));
}

[TestMethod]
public async Task ExecuteQuery_QueryWithToken()
{
Expand Down Expand Up @@ -163,4 +176,24 @@ public async Task InvalidDomain_ThrowTest()

await Salesforce.ExecuteQuery(input, options, _cancellationToken);
}

[TestMethod]
public async Task InvalidQuery_ReturnsError()
{
var input = new Input
{
Domain = _domain,
Query = "SELECT NAME from Invalid"
};

var options = new Options
{
AuthenticationMethod = AuthenticationMethod.AccessToken,
AccessToken = await Salesforce.GetAccessToken(_authurl, _clientID, _clientSecret, _username, _password + _securityToken, _cancellationToken)
};

var result = await Salesforce.ExecuteQuery(input, options, _cancellationToken);
Assert.IsNotNull(result.ErrorMessage);
StringAssert.Contains(result.ErrorMessage, "sObject type 'Invalid' is not supported.");
}
}
Loading

0 comments on commit ae5b69c

Please sign in to comment.