Skip to content
Snippets Groups Projects
SenderTestBase.cs 2.19 KiB
using System;
using System.IO;
using System.Linq;
using FitConnect;
using FitConnect.Interfaces.Sender;
using FitConnect.Models;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using NUnit.Framework;

namespace IntegrationTests.Sender;

[Order(100)]
[TestFixture]
public abstract class SenderTestBase {
    /// <summary>
    ///     Setup creates json file for credentials if not found
    /// </summary>
    /// <exception cref="Exception"></exception>
    [OneTimeSetUp]
    public void OneTimeSetup() {
        // relative to the project execution directory
        const string secretFile = "../../../http-client.env.json";

        if (!File.Exists(secretFile)) {
            // If the secret file is not found, create it with the default values
            // The file will be pretty in C#11, when """ is introduced
            File.WriteAllText(secretFile, @"
{
    ""sender"": {
        ""id"": ""00000000-0000-0000-0000-000000000000"",
        ""secret"": ""0000000000000000000000000000000000000000000"",
        ""scope"": ""send:region:DE""
    }
}");
            throw new Exception("Please fill the secret.json file with your sender credentials");
        }

        var jsonContent = File.ReadAllText(secretFile);
        var secret = JsonConvert.DeserializeObject<dynamic>(jsonContent)!;
        ClientId = secret.sender.id;
        ClientSecret = secret.sender.secret;
    }

    [SetUp]
    public void Setup() {
        var logger = LoggerFactory.Create(b => b.AddConsole()).CreateLogger<FitConnect.Sender>();
        Sender = new FitConnect.Sender(
            FitConnectEnvironment.Testing,
            ClientId, ClientSecret, logger);
    }

    protected string LeikaKey = "";

    protected const string DestinationId = "aa3704d6-8bd7-4d40-a8af-501851f93934";
    protected string ClientId = "73a8ff88-076b-4263-9a80-8ebadac97b0d";
    protected string ClientSecret = "rdlXms-4ikO47AbTmmCTdzFoE4cTSt13JmSbcY5Dhsw";
    protected ISender Sender = null!;

    protected Submission GetSubmissionInfo(ISender sender) {
        var submission = sender.GetType().GetProperties()
            .FirstOrDefault(p => p.Name == "Submission")!
            .GetValue(Sender) as Submission;

        return submission!;
    }
}