-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
TestEnvironmentTests.cs
121 lines (102 loc) · 4.66 KB
/
TestEnvironmentTests.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.IO;
using Azure.Core.TestFramework;
using NUnit.Framework;
namespace Azure.Core.Tests
{
public class TestEnvironmentTests
{
[SetUp]
public void SetUp()
{
Environment.SetEnvironmentVariable("CORE_RECORDED", "1");
Environment.SetEnvironmentVariable("CORE_NOTRECORDED", "2");
Environment.SetEnvironmentVariable("CORE_Base64Secret", "1");
Environment.SetEnvironmentVariable("CORE_CustomSecret", "1");
Environment.SetEnvironmentVariable("CORE_DefaultSecret", "1");
Environment.SetEnvironmentVariable("CORE_ConnectionStringWithSecret", "endpoint=1;key=2");
}
[Theory]
[TestCase(RecordedTestMode.Live)]
[TestCase(RecordedTestMode.Playback)]
[TestCase(RecordedTestMode.Record)]
[TestCase(RecordedTestMode.None)]
public void ReadingRecordedValueInCtorThrows(RecordedTestMode mode)
{
Assert.Throws<InvalidOperationException>(() => new RecordedVariableMisuse(true, mode));
}
[Theory]
[TestCase(RecordedTestMode.Live)]
[TestCase(RecordedTestMode.Playback)]
[TestCase(RecordedTestMode.Record)]
[TestCase(RecordedTestMode.None)]
public void ReadingNonRecordedValueInCtorWorks(RecordedTestMode mode)
{
var test = new RecordedVariableMisuse(false, mode);
Assert.AreEqual("2", test.Value);
}
[Test]
public void ReadingRecordedValueOutsideRecordedTestWorks()
{
Assert.AreEqual("1", MockTestEnvironment.Instance.RecordedValue);
}
[Test]
public void ReadingNonRecordedValueOutsideRecordedTestWorks()
{
Assert.AreEqual("2", MockTestEnvironment.Instance.NotRecordedValue);
}
[Test]
public void RecordedVariableSanitized()
{
var tempFile = Path.GetTempFileName();
var env = new MockTestEnvironment();
var testRecording = new TestRecording(RecordedTestMode.Record, tempFile, new RecordedTestSanitizer(), new RecordMatcher());
env.Mode = RecordedTestMode.Record;
env.SetRecording(testRecording);
Assert.AreEqual("1", env.Base64Secret);
Assert.AreEqual("1", env.CustomSecret);
Assert.AreEqual("1", env.DefaultSecret);
Assert.AreEqual("endpoint=1;key=2", env.ConnectionStringWithSecret);
testRecording.Dispose();
testRecording = new TestRecording(RecordedTestMode.Playback, tempFile, new RecordedTestSanitizer(), new RecordMatcher());
Assert.AreEqual("Kg==", testRecording.GetVariable("Base64Secret", ""));
Assert.AreEqual("Custom", testRecording.GetVariable("CustomSecret", ""));
Assert.AreEqual("Sanitized", testRecording.GetVariable("DefaultSecret", ""));
Assert.AreEqual("endpoint=1;key=Sanitized", testRecording.GetVariable("ConnectionStringWithSecret", ""));
}
private class RecordedVariableMisuse : RecordedTestBase<MockTestEnvironment>
{
// To make NUnit happy
public RecordedVariableMisuse(bool isAsync) : base(isAsync)
{
}
public RecordedVariableMisuse(bool recorded, RecordedTestMode mode) : base(true, mode)
{
if (recorded)
{
Value = TestEnvironment.RecordedValue;
}
else
{
Value = TestEnvironment.NotRecordedValue;
}
}
public string Value { get; }
}
private class MockTestEnvironment: TestEnvironment
{
public MockTestEnvironment(): base("core")
{
}
public static MockTestEnvironment Instance { get; } = new MockTestEnvironment();
public string RecordedValue => GetRecordedVariable("RECORDED");
public string NotRecordedValue => GetVariable("NOTRECORDED");
public string Base64Secret => GetRecordedVariable("Base64Secret", option => option.IsSecret(SanitizedValue.Base64));
public string DefaultSecret => GetRecordedVariable("DefaultSecret", option => option.IsSecret(SanitizedValue.Default));
public string CustomSecret => GetRecordedVariable("CustomSecret", option => option.IsSecret("Custom"));
public string ConnectionStringWithSecret => GetRecordedVariable("ConnectionStringWithSecret", option => option.HasSecretConnectionStringParameter("key"));
}
}
}