-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathOktaConfigShould.cs
More file actions
173 lines (135 loc) · 7.86 KB
/
Copy pathOktaConfigShould.cs
File metadata and controls
173 lines (135 loc) · 7.86 KB
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
164
165
166
167
168
169
170
171
172
173
// <copyright file="OktaConfigShould.cs" company="Okta, Inc">
// Copyright (c) 2019-present Okta, Inc. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
// </copyright>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Okta.Xamarin;
using Xunit;
namespace Okta.Xamarin.Test
{
public class OktaConfigShould
{
[Fact]
public void ConfigValidatorPassesWhenValid()
{
OktaConfigValidator<IOktaConfig> validator = new OktaConfigValidator<IOktaConfig>();
validator.Validate(
new OktaConfig("testoktaid", "https://dev-00000.oktapreview.com", "com.test:/redirect", "com.test:/logout"));
validator.Validate(
new OktaConfig("testoktaid", "https://okta.okta.com", "appid:/redirect", "appid:/logout")
{ ClockSkew = TimeSpan.FromSeconds(100), Scope = "test1 test2" });
// Test new TLD support
validator.Validate(
new OktaConfig("testoktaid", "https://dev-00000.trex-govcloud.com", "com.test:/redirect", "com.test:/logout"));
validator.Validate(
new OktaConfig("testoktaid", "https://dev-00000.okta-gov.com", "com.test:/redirect", "com.test:/logout"));
validator.Validate(
new OktaConfig("testoktaid", "https://dev-00000.okta.mil", "com.test:/redirect", "com.test:/logout"));
validator.Validate(
new OktaConfig("testoktaid", "https://dev-00000.okta-miltest.com", "com.test:/redirect", "com.test:/logout"));
// The validator throws an exception when the config is invalid, so if we got here without an exception then the configs are valid.
}
[Fact]
public void ConfigValidatorCatchesMissingProperties()
{
OktaConfigValidator<OktaConfig> validator = new OktaConfigValidator<OktaConfig>();
Assert.Throws<ArgumentNullException>("config", () =>
validator.Validate(null));
Assert.Throws<ArgumentNullException>(() =>
validator.Validate(new OktaConfig()));
Assert.Throws<ArgumentNullException>("ClientId", () =>
validator.Validate(new OktaConfig(null, "https://dev-00000.oktapreview.com", "com.test:/redirect", "com.test:/logout")));
Assert.Throws<ArgumentNullException>("OktaDomain", () =>
validator.Validate(new OktaConfig("testoktaid", null, "com.test:/redirect", "com.test:/logout")));
Assert.Throws<ArgumentNullException>("RedirectUri", () =>
validator.Validate(new OktaConfig("testoktaid", "https://dev-00000.oktapreview.com", null, "com.test:/logout")));
Assert.Throws<ArgumentNullException>("PostLogoutRedirectUri", () =>
validator.Validate(new OktaConfig("testoktaid", "https://dev-00000.oktapreview.com", "com.test:/redirect", null)));
}
[Fact]
public void ConfigValidatorCatchesInvalidDomain()
{
OktaConfigValidator<OktaConfig> validator = new OktaConfigValidator<OktaConfig>();
Assert.Throws<ArgumentException>(() =>
validator.Validate(new OktaConfig("testoktaid", "http://dev-00000.oktapreview.com", "com.test:/redirect", "com.test:/logout")));
Assert.Throws<ArgumentException>(() =>
validator.Validate(new OktaConfig("testoktaid", "https://dev-00000-admin.oktapreview.com", "com.test:/redirect", "com.test:/logout")));
// Test that admin domains are caught for new TLDs
Assert.Throws<ArgumentException>(() =>
validator.Validate(new OktaConfig("testoktaid", "https://dev-00000-admin.trex-govcloud.com", "com.test:/redirect", "com.test:/logout")));
Assert.Throws<ArgumentException>(() =>
validator.Validate(new OktaConfig("testoktaid", "https://dev-00000-admin.okta-gov.com", "com.test:/redirect", "com.test:/logout")));
Assert.Throws<ArgumentException>(() =>
validator.Validate(new OktaConfig("testoktaid", "https://dev-00000-admin.okta.mil", "com.test:/redirect", "com.test:/logout")));
Assert.Throws<ArgumentException>(() =>
validator.Validate(new OktaConfig("testoktaid", "https://dev-00000-admin.okta-miltest.com", "com.test:/redirect", "com.test:/logout")));
Assert.Throws<ArgumentException>(() =>
validator.Validate(new OktaConfig("testoktaid", "https://{yourOktaDomain}", "com.test:/redirect", "com.test:/logout")));
Assert.Throws<ArgumentException>(() =>
validator.Validate(new OktaConfig("testoktaid", "dev-00000.oktapreview.com", "com.test:/redirect", "com.test:/logout")));
}
[Fact]
public async Task ParseJsonFull()
{
var tempConfigFile = new FileInfo(Path.GetTempFileName());
File.WriteAllText(tempConfigFile.FullName, @"{
""ClientId"": ""testoktaid"",
""OktaDomain"": ""https://dev-00000.oktapreview.com"",
""RedirectUri"": ""com.test:/redirect"",
""PostLogoutRedirectUri"": ""com.test:/logout"",
""Scope"": ""test1 test2 test3"",
""AuthorizationServerId"": ""test1"",
""ClockSkew"": 90
}");
OktaConfig config = await OktaConfig.LoadFromJsonFileAsync(tempConfigFile.FullName);
Assert.Equal("testoktaid", config.ClientId);
Assert.Equal("https://dev-00000.oktapreview.com", config.OktaDomain);
Assert.Equal("com.test:/redirect", config.RedirectUri);
Assert.Equal("com.test:/logout", config.PostLogoutRedirectUri);
Assert.Equal("test1 test2 test3", config.Scope);
Assert.Equal((IEnumerable<string>)(new string[] { "test1", "test2", "test3" }), config.Scopes);
Assert.Equal("test1", config.AuthorizationServerId);
Assert.Equal(TimeSpan.FromSeconds(90), config.ClockSkew);
try
{
tempConfigFile.Delete();
}
catch (Exception ex)
{
Debug.WriteLine("Unable to clean up temp file used for testing OktaConfig JSON file parsing at " + tempConfigFile.FullName + Environment.NewLine + ex.ToString());
}
}
[Fact]
public async Task ParseJsonMinimal()
{
var tempConfigFile = new FileInfo(Path.GetTempFileName());
File.WriteAllText(tempConfigFile.FullName, @"{
""ClientId"": ""testoktaid"",
""OktaDomain"": ""https://dev-00000.oktapreview.com"",
""RedirectUri"": ""com.test:/redirect"",
""PostLogoutRedirectUri"": ""com.test:/logout""
}");
OktaConfig config = await OktaConfig.LoadFromJsonFileAsync(tempConfigFile.FullName);
Assert.Equal("testoktaid", config.ClientId);
Assert.Equal("https://dev-00000.oktapreview.com", config.OktaDomain);
Assert.Equal("com.test:/redirect", config.RedirectUri);
Assert.Equal("com.test:/logout", config.PostLogoutRedirectUri);
Assert.Equal("openid profile", config.Scope);
Assert.Equal((IEnumerable<string>)(new string[] { "openid", "profile" }), config.Scopes);
Assert.Equal("default", config.AuthorizationServerId);
Assert.Equal(TimeSpan.FromSeconds(120), config.ClockSkew);
try
{
tempConfigFile.Delete();
}
catch (Exception ex)
{
Debug.WriteLine("Unable to clean up temp file used for testing OktaConfig JSON file parsing at " + tempConfigFile.FullName + Environment.NewLine + ex.ToString());
}
}
}
}