-
Notifications
You must be signed in to change notification settings - Fork 430
/
Copy pathAuthTests.cs
127 lines (108 loc) · 3.61 KB
/
AuthTests.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
using NUnit.Framework;
using System;
using System.IO;
using System.Threading.Tasks;
namespace OpenAI_Tests
{
public class AuthTests
{
[SetUp]
public void Setup()
{
File.WriteAllText(".openai", "OPENAI_KEY=pk-test12" + Environment.NewLine + "OPENAI_ORGANIZATION=org-testing123");
Environment.SetEnvironmentVariable("OPENAI_API_KEY", "pk-test-env");
Environment.SetEnvironmentVariable("OPENAI_ORGANIZATION", "org-testing123");
}
[Test]
public void GetAuthFromEnv()
{
var auth = OpenAI_API.APIAuthentication.LoadFromEnv();
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.ApiKey);
Assert.IsNotEmpty(auth.ApiKey);
Assert.AreEqual("pk-test-env", auth.ApiKey);
}
[Test]
public void GetAuthFromFile()
{
var auth = OpenAI_API.APIAuthentication.LoadFromPath();
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.ApiKey);
Assert.AreEqual("pk-test12", auth.ApiKey);
}
[Test]
public void GetAuthFromNonExistantFile()
{
var auth = OpenAI_API.APIAuthentication.LoadFromPath(filename: "bad.config");
Assert.IsNull(auth);
}
[Test]
public void GetDefault()
{
var auth = OpenAI_API.APIAuthentication.Default;
var envAuth = OpenAI_API.APIAuthentication.LoadFromEnv();
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.ApiKey);
Assert.IsNotNull(envAuth);
Assert.IsNotNull(envAuth.ApiKey);
Assert.AreEqual(envAuth.ApiKey, auth.ApiKey);
Assert.IsNotNull(auth.OpenAIOrganization);
Assert.IsNotNull(envAuth.OpenAIOrganization);
Assert.AreEqual(envAuth.OpenAIOrganization, auth.OpenAIOrganization);
}
[Test]
public void testHelper()
{
OpenAI_API.APIAuthentication defaultAuth = OpenAI_API.APIAuthentication.Default;
OpenAI_API.APIAuthentication manualAuth = new OpenAI_API.APIAuthentication("pk-testAA");
OpenAI_API.OpenAIAPI api = new OpenAI_API.OpenAIAPI();
OpenAI_API.APIAuthentication shouldBeDefaultAuth = api.Auth;
Assert.IsNotNull(shouldBeDefaultAuth);
Assert.IsNotNull(shouldBeDefaultAuth.ApiKey);
Assert.AreEqual(defaultAuth.ApiKey, shouldBeDefaultAuth.ApiKey);
OpenAI_API.APIAuthentication.Default = new OpenAI_API.APIAuthentication("pk-testAA");
api = new OpenAI_API.OpenAIAPI();
OpenAI_API.APIAuthentication shouldBeManualAuth = api.Auth;
Assert.IsNotNull(shouldBeManualAuth);
Assert.IsNotNull(shouldBeManualAuth.ApiKey);
Assert.AreEqual(manualAuth.ApiKey, shouldBeManualAuth.ApiKey);
}
[Test]
public void GetKey()
{
var auth = new OpenAI_API.APIAuthentication("pk-testAA");
Assert.IsNotNull(auth.ApiKey);
Assert.AreEqual("pk-testAA", auth.ApiKey);
}
[Test]
public void ParseKey()
{
var auth = new OpenAI_API.APIAuthentication("pk-testAA");
Assert.IsNotNull(auth.ApiKey);
Assert.AreEqual("pk-testAA", auth.ApiKey);
Assert.IsNull(auth.OpenAIOrganization);
auth = "pk-testCC";
Assert.IsNotNull(auth.ApiKey);
Assert.AreEqual("pk-testCC", auth.ApiKey);
auth = new OpenAI_API.APIAuthentication("sk-testBB", "orgTest");
Assert.IsNotNull(auth.ApiKey);
Assert.AreEqual("sk-testBB", auth.ApiKey);
Assert.IsNotNull(auth.OpenAIOrganization);
Assert.AreEqual("orgTest", auth.OpenAIOrganization);
}
[Test]
public async Task TestBadKey()
{
var auth = new OpenAI_API.APIAuthentication("pk-testAA");
Assert.IsFalse(await auth.ValidateAPIKeyAsync());
auth = new OpenAI_API.APIAuthentication(null);
Assert.IsFalse(await auth.ValidateAPIKeyAsync());
}
[Test]
public async Task TestValidateGoodKey()
{
var auth = new OpenAI_API.APIAuthentication(Environment.GetEnvironmentVariable("TEST_OPENAI_SECRET_KEY"));
Assert.IsTrue(await auth.ValidateAPIKeyAsync());
}
}
}