Skip to content

Commit 66edadc

Browse files
committed
Add tests.
1 parent a3c2c13 commit 66edadc

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace Microsoft.Graph.Authentication.Test.Helpers
2+
{
3+
using Microsoft.Graph.PowerShell.Authentication;
4+
using System;
5+
using Xunit;
6+
public class GraphSessionTests
7+
{
8+
[Fact]
9+
public void GraphSessionShouldBeInitilizedAfterInitializerIsCalled()
10+
{
11+
GraphSessionInitializer.InitializeSession();
12+
13+
Assert.NotNull(GraphSession.Instance);
14+
Assert.Null(GraphSession.Instance.AuthContext);
15+
}
16+
17+
[Fact]
18+
public void ShoudlOverwriteExistingGraphSession()
19+
{
20+
GraphSessionInitializer.InitializeSession();
21+
Guid originalSessionId = GraphSession.Instance._graphSessionId;
22+
23+
GraphSession.Initialize(() => new GraphSession(), true);
24+
25+
Assert.NotNull(GraphSession.Instance);
26+
Assert.NotEqual(GraphSession.Instance._graphSessionId, originalSessionId);
27+
}
28+
29+
[Fact]
30+
public void ShoudlNotOverwriteExistingGraphSession()
31+
{
32+
GraphSessionInitializer.InitializeSession();
33+
Guid originalSessionId = GraphSession.Instance._graphSessionId;
34+
35+
GraphSession.Initialize(() => new GraphSession());
36+
37+
Assert.NotNull(GraphSession.Instance);
38+
Assert.Equal(GraphSession.Instance._graphSessionId, originalSessionId);
39+
}
40+
}
41+
}

src/Authentication/Authentication/Common/GraphSession.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ namespace Microsoft.Graph.PowerShell.Authentication
66
{
77
using System;
88
using System.Threading;
9+
/// <summary>
10+
/// The current <see cref="GraphSession"/>.
11+
/// </summary>
912
public class GraphSession: IGraphSession
1013
{
1114
static GraphSession _instance;
1215
static bool _initialized = false;
1316
static ReaderWriterLockSlim sessionLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
17+
internal Guid _graphSessionId;
18+
19+
/// <summary>
20+
/// Gets or Sets <see cref="IAuthContext"/>.
21+
/// </summary>
1422
public IAuthContext AuthContext { get; set; }
1523
public static GraphSession Instance
1624
{
@@ -42,7 +50,16 @@ public static GraphSession Instance
4250
}
4351
}
4452
}
53+
public GraphSession()
54+
{
55+
_graphSessionId = Guid.NewGuid();
56+
}
4557

58+
/// <summary>
59+
/// Initialize <see cref="GraphSession"/>.
60+
/// </summary>
61+
/// <param name="instanceCreator">A func to create an instance.</param>
62+
/// <param name="overwrite">If true, overwrite the current instance. Otherwise do not initialize.</param>
4663
public static void Initialize(Func<GraphSession> instanceCreator, bool overwrite)
4764
{
4865
try
@@ -71,11 +88,19 @@ public static void Initialize(Func<GraphSession> instanceCreator, bool overwrite
7188
}
7289
}
7390

91+
/// <summary>
92+
/// Initialize the current instance if none exists.
93+
/// </summary>
94+
/// <param name="instanceCreator">A func to create an instance.</param>
7495
public static void Initialize(Func<GraphSession> instanceCreator)
7596
{
7697
Initialize(instanceCreator, false);
7798
}
7899

100+
/// <summary>
101+
/// Modify the current instance of <see cref="GraphSession"/>.
102+
/// </summary>
103+
/// <param name="modifier">A func to modify the <see cref="GraphSession"/> instance.</param>
79104
public static void Modify(Action<GraphSession> modifier)
80105
{
81106
try

src/Authentication/Authentication/Common/GraphSessionInitializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static void InitializeSession()
1515
}
1616

1717
/// <summary>
18-
/// Creates or gets a instance of a <see cref="GraphSession"/>.
18+
/// Creates a new instance of a <see cref="GraphSession"/>.
1919
/// </summary>
2020
/// <returns><see cref="GraphSession"/></returns>
2121
internal static GraphSession CreateInstance()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using System.Runtime.CompilerServices;
2+
3+
#if DEBUG
4+
[assembly: InternalsVisibleTo("Microsoft.Graph.Authentication.Test")]
5+
#endif

0 commit comments

Comments
 (0)