generated from RageAgainstThePixel/upm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestFixture_00_Authentication.cs
163 lines (139 loc) · 5.46 KB
/
TestFixture_00_Authentication.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// 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 = new BlockadeLabsAuthentication().LoadFromEnvironment();
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.Info.ApiKey);
Assert.IsNotEmpty(auth.Info.ApiKey);
}
[Test]
public void Test_02_GetAuthFromFile()
{
var auth = new BlockadeLabsAuthentication().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 = new BlockadeLabsAuthentication().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 configuration = AssetDatabase.LoadAssetAtPath<BlockadeLabsConfiguration>(configPath);
Assert.IsNotNull(configuration);
var auth = new BlockadeLabsAuthentication().LoadFromAsset(configuration);
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.Info.ApiKey);
Assert.IsNotEmpty(auth.Info.ApiKey);
Assert.AreEqual(auth.Info.ApiKey, configuration.ApiKey);
if (cleanup)
{
AssetDatabase.DeleteAsset(configPath);
AssetDatabase.DeleteAsset("Assets/Resources");
}
}
[Test]
public void Test_05_Authentication()
{
var defaultAuth = BlockadeLabsAuthentication.Default;
Assert.IsNotNull(defaultAuth);
Assert.IsNotNull(defaultAuth.Info.ApiKey);
Assert.AreEqual(defaultAuth.Info.ApiKey, BlockadeLabsAuthentication.Default.Info.ApiKey);
var manualAuth = 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);
Debug.Log(api.Settings.Info.BaseRequestUrlFormat);
}
[TearDown]
public void TearDown()
{
if (File.Exists(BlockadeLabsAuthentication.CONFIG_FILE))
{
File.Delete(BlockadeLabsAuthentication.CONFIG_FILE);
}
BlockadeLabsSettings.Default = null;
BlockadeLabsAuthentication.Default = null;
}
}
}