Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

com.rest.blockadelabs 1.0.0-preview.1 #1

Merged
merged 18 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixed namespaces
updated unit tests for auth
  • Loading branch information
StephenHodgson committed May 28, 2023
commit d23f0091e5acbfed6ad7c37f0e3018a3bdbe08e4
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Rest.BlockadeLabs;
using BlockadeLabs;
using System;
using UnityEditor;
using Utilities.Rest.Editor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("BlockadeLabs.Tests")]
[assembly: InternalsVisibleTo("BlockadeLabs.Editor")]
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnityEngine;
using Utilities.WebRequestRest.Interfaces;

namespace Rest.BlockadeLabs
namespace BlockadeLabs
{
[Serializable]
public class BlockadeLabsAuthInfo : IAuthInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using UnityEngine;
using Utilities.WebRequestRest.Interfaces;

namespace Rest.BlockadeLabs
namespace BlockadeLabs
{
public sealed class BlockadeLabsAuthentication : AbstractAuthentication<BlockadeLabsAuthentication, BlockadeLabsAuthInfo>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Utilities.WebRequestRest;

namespace Rest.BlockadeLabs
namespace BlockadeLabs
{
public abstract class BlockadeLabsBaseEndpoint : BaseEndPoint<BlockadeLabsClient, BlockadeLabsAuthentication, BlockadeLabsSettings>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Security.Authentication;
using Utilities.WebRequestRest;

namespace Rest.BlockadeLabs
namespace BlockadeLabs
{
public class BlockadeLabsClient : BaseClient<BlockadeLabsAuthentication, BlockadeLabsSettings>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using UnityEngine;
using Utilities.WebRequestRest.Interfaces;

namespace Rest.BlockadeLabs
namespace BlockadeLabs
{
[CreateAssetMenu(fileName = nameof(BlockadeLabsConfiguration), menuName = nameof(BlockadeLabs) + "/" + nameof(BlockadeLabsConfiguration), order = 0)]
public class BlockadeLabsConfiguration : ScriptableObject, IConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using UnityEngine;
using Utilities.WebRequestRest.Interfaces;

namespace Rest.BlockadeLabs
namespace BlockadeLabs
{
public class BlockadeLabsSettings : ISettings
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Utilities.WebRequestRest.Interfaces;

namespace Rest.BlockadeLabs
namespace BlockadeLabs
{
public class BlockadeLabsSettingsInfo : ISettingsInfo
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading.Tasks;

namespace Rest.BlockadeLabs
namespace BlockadeLabs
{
public sealed class RootEndpoint : BlockadeLabsBaseEndpoint
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"rootNamespace": "BlockadeLabs.Tests",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner"
"UnityEditor.TestRunner",
"BlockadeLabs",
"Utilities.Rest"
],
"includePlatforms": [
"Editor"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using NUnit.Framework;
using System;
using System.IO;
using System.Security.Authentication;
using UnityEditor;
using UnityEngine;

namespace BlockadeLabs.Tests
{
internal class TestFixture_00_Authentication
{
[SetUp]
public void Setup()
{
var authJson = new BlockadeLabsAuthInfo("key-test12");
var authText = JsonUtility.ToJson(authJson, true);
File.WriteAllText(BlockadeLabsAuthentication.CONFIG_FILE, authText);
}

[Test]
public void Test_01_GetAuthFromEnv()
{
var auth = BlockadeLabsAuthentication.Default.LoadFromEnvironment();
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.Info.ApiKey);
Assert.IsNotEmpty(auth.Info.ApiKey);
}

[Test]
public void Test_02_GetAuthFromFile()
{
var auth = BlockadeLabsAuthentication.Default.LoadFromPath(Path.GetFullPath(BlockadeLabsAuthentication.CONFIG_FILE));
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.Info.ApiKey);
Assert.AreEqual("key-test12", auth.Info.ApiKey);
}

[Test]
public void Test_03_GetAuthFromNonExistentFile()
{
var auth = BlockadeLabsAuthentication.Default.LoadFromDirectory(filename: "bad.config");
Assert.IsNull(auth);
}

[Test]
public void Test_04_GetAuthFromConfiguration()
{
var configPath = $"Assets/Resources/{nameof(BlockadeLabsConfiguration)}.asset";
var cleanup = false;

if (!File.Exists(Path.GetFullPath(configPath)))
{
if (!Directory.Exists($"{Application.dataPath}/Resources"))
{
Directory.CreateDirectory($"{Application.dataPath}/Resources");
}

var instance = ScriptableObject.CreateInstance<BlockadeLabsConfiguration>();
instance.ApiKey = "key-test12";
AssetDatabase.CreateAsset(instance, configPath);
cleanup = true;
}

var config = AssetDatabase.LoadAssetAtPath<BlockadeLabsConfiguration>(configPath);
var auth = BlockadeLabsAuthentication.Default.LoadFromAsset<BlockadeLabsConfiguration>();

Assert.IsNotNull(auth);
Assert.IsNotNull(auth.Info.ApiKey);
Assert.IsNotEmpty(auth.Info.ApiKey);
Assert.AreEqual(auth.Info.ApiKey, config.ApiKey);

if (cleanup)
{
AssetDatabase.DeleteAsset(configPath);
AssetDatabase.DeleteAsset("Assets/Resources");
}
}

[Test]
public void Test_05_Authentication()
{
var defaultAuth = BlockadeLabsAuthentication.Default;
var manualAuth = new BlockadeLabsAuthentication("key-testAA");

Assert.IsNotNull(defaultAuth);
Assert.IsNotNull(defaultAuth.Info.ApiKey);
Assert.AreEqual(defaultAuth.Info.ApiKey, BlockadeLabsAuthentication.Default.Info.ApiKey);

BlockadeLabsAuthentication.Default = new BlockadeLabsAuthentication("key-testAA");
Assert.IsNotNull(manualAuth);
Assert.IsNotNull(manualAuth.Info.ApiKey);
Assert.AreEqual(manualAuth.Info.ApiKey, BlockadeLabsAuthentication.Default.Info.ApiKey);

BlockadeLabsAuthentication.Default = defaultAuth;
}

[Test]
public void Test_06_GetKey()
{
var auth = new BlockadeLabsAuthentication("key-testAA");
Assert.IsNotNull(auth.Info.ApiKey);
Assert.AreEqual("key-testAA", auth.Info.ApiKey);
}

[Test]
public void Test_07_GetKeyFailed()
{
BlockadeLabsAuthentication auth = null;

try
{
auth = new BlockadeLabsAuthentication("fail-key");
}
catch (InvalidCredentialException)
{
Assert.IsNull(auth);
}
catch (Exception e)
{
Assert.IsTrue(false, $"Expected exception {nameof(InvalidCredentialException)} but got {e.GetType().Name}");
}
}

[Test]
public void Test_08_ParseKey()
{
var auth = new BlockadeLabsAuthentication("key-testAA");
Assert.IsNotNull(auth.Info.ApiKey);
Assert.AreEqual("key-testAA", auth.Info.ApiKey);
auth = "key-testCC";
Assert.IsNotNull(auth.Info.ApiKey);
Assert.AreEqual("key-testCC", auth.Info.ApiKey);

auth = new BlockadeLabsAuthentication("key-testBB");
Assert.IsNotNull(auth.Info.ApiKey);
Assert.AreEqual("key-testBB", auth.Info.ApiKey);
}

[Test]
public void Test_12_CustomDomainConfigurationSettings()
{
var auth = new BlockadeLabsAuthentication("customIssuedToken");
var settings = new BlockadeLabsSettings(domain: "api.your-custom-domain.com");
var api = new BlockadeLabsClient(auth, settings);
Console.WriteLine(api.Settings.Info.BaseRequestUrlFormat);
}

[TearDown]
public void TearDown()
{
if (File.Exists(BlockadeLabsAuthentication.CONFIG_FILE))
{
File.Delete(BlockadeLabsAuthentication.CONFIG_FILE);
}
}
}
}