|
| 1 | +// Licensed to the Symphony Software Foundation (SSF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SSF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +namespace SymphonyOSS.RestApiClient.Tests |
| 19 | +{ |
| 20 | + using Authentication; |
| 21 | + using Generated.OpenApi.AuthenticatorApi.Api; |
| 22 | + using Generated.OpenApi.AuthenticatorApi.Model; |
| 23 | + using Moq; |
| 24 | + using System.Security.Cryptography.X509Certificates; |
| 25 | + using Xunit; |
| 26 | + |
| 27 | + public class AppSessionManagerTest |
| 28 | + { |
| 29 | + private readonly X509Certificate2 _certificate; |
| 30 | + |
| 31 | + private readonly Mock<IAuthenticationApi> _sessionAuthApiMock; |
| 32 | + |
| 33 | + public AppSessionManagerTest() |
| 34 | + { |
| 35 | + _certificate = new Mock<X509Certificate2>().Object; |
| 36 | + _sessionAuthApiMock = new Mock<IAuthenticationApi>(); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void EnsureTokens_are_returned_without_explicitly_calling_GenerateTokens() |
| 41 | + { |
| 42 | + _sessionAuthApiMock.Setup(obj => obj.V1AppAuthenticatePost()).Returns(new Token("appSessionToken", "as1")); |
| 43 | + _sessionAuthApiMock.Setup(obj => obj.V1AppUsernameUsernameAuthenticatePost(null, "as1")).Returns(new Token("userSessionToken", "us1")); |
| 44 | + var appSessionManager = new AppSessionManager(_sessionAuthApiMock.Object, _certificate); |
| 45 | + var sessionToken = appSessionManager.SessionToken; |
| 46 | + var keyManagerToken = appSessionManager.KeyManagerToken; |
| 47 | + Assert.Equal("as1", sessionToken); |
| 48 | + Assert.Null(keyManagerToken); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public void EnsureGenerateTokens_regenerate_tokens_every_call() |
| 53 | + { |
| 54 | + var sessionTokenCounter = 0; |
| 55 | + _sessionAuthApiMock.Setup(obj => obj.V1AppAuthenticatePost()).Returns(() => |
| 56 | + { |
| 57 | + ++sessionTokenCounter; |
| 58 | + return new Token("appSessionToken", "as" + sessionTokenCounter); |
| 59 | + }); |
| 60 | + _sessionAuthApiMock.Setup(obj => obj.V1AppUsernameUsernameAuthenticatePost(null, It.IsAny<string>())).Returns(new Token("userSessionToken", "us1")); |
| 61 | + var appSessionManager = new AppSessionManager(_sessionAuthApiMock.Object, _certificate); |
| 62 | + Assert.Equal("as1", appSessionManager.SessionToken); |
| 63 | + Assert.Equal("as1", appSessionManager.SessionToken); |
| 64 | + Assert.Null(appSessionManager.KeyManagerToken); |
| 65 | + |
| 66 | + appSessionManager.GenerateTokens(); |
| 67 | + Assert.Equal("as2", appSessionManager.SessionToken); |
| 68 | + Assert.Null(appSessionManager.KeyManagerToken); |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | +} |
0 commit comments