|
| 1 | +// Copyright (c) Avanade Inc. All rights reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.IdentityModel.Tokens.Jwt; |
| 7 | +using System.Linq; |
| 8 | +using System.Security.Claims; |
| 9 | +using System.Security.Cryptography; |
| 10 | +using System.Security.Cryptography.X509Certificates; |
| 11 | +using System.Security.Principal; |
| 12 | +using System.Text; |
| 13 | +using Liquid.Base; |
| 14 | +using Liquid.Interfaces; |
| 15 | +using Microsoft.AspNetCore.Authorization; |
| 16 | +using Microsoft.IdentityModel.Tokens; |
| 17 | +using Newtonsoft.Json; |
| 18 | +using NSubstitute; |
| 19 | +using Xunit; |
| 20 | + |
| 21 | +namespace Liquid.Activation.Tests |
| 22 | +{ |
| 23 | + public class LightWorkerTests |
| 24 | + { |
| 25 | + public LightWorkerTests() |
| 26 | + { |
| 27 | + Workbench.Instance.Reset(); |
| 28 | + Workbench.Instance.AddToCache(WorkbenchServiceType.Telemetry, Substitute.For<ILightTelemetry>()); |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void InitializeWhenMockLightWorkerPresentThenQueueAndTopicsAreDiscovered() |
| 33 | + { |
| 34 | + var sut = new MockLightWorker(); |
| 35 | + sut.Initialize(); |
| 36 | + |
| 37 | + Assert.Contains( |
| 38 | + MockLightWorker.TopicList, |
| 39 | + _ => _.MethodInfo.ReflectedType == typeof(MockLightWorker) |
| 40 | + && _.MethodInfo.Name == nameof(MockLightWorker.TopicMethod)); |
| 41 | + |
| 42 | + Assert.Contains( |
| 43 | + MockLightWorker.QueueList, |
| 44 | + _ => _.MethodInfo.ReflectedType == typeof(MockLightWorker) |
| 45 | + && _.MethodInfo.Name == nameof(MockLightWorker.QueueMethod)); |
| 46 | + |
| 47 | + // Given the static nature of LightWorker, we couldn't make this an isolated assertion |
| 48 | + // TODO: Refactor LightWorker and then make this isolated |
| 49 | + Assert.Throws<LightException>(() => new MockLightWorker().Initialize()); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void InvokeProcessWhenMessageIsntValidJsonThrows() |
| 54 | + { |
| 55 | + var actual = LightWorker.InvokeProcess(null, null); |
| 56 | + Assert.Null(actual); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void InvokeProcessWhenMessageIsValidJsonParsesItCorrectly() |
| 61 | + { |
| 62 | + // ARRANGE |
| 63 | + var anonymous = new Foobar { Foo = "Bar" }; |
| 64 | + var anonymousAsByteStream = ToJsonByteStream(anonymous); |
| 65 | + |
| 66 | + var method = typeof(MethodsCollection).GetMethod(nameof(MethodsCollection.FoobarMethod)); |
| 67 | + |
| 68 | + // ACT |
| 69 | + var actual = (Foobar)LightWorker.InvokeProcess(method, anonymousAsByteStream); |
| 70 | + |
| 71 | + // ASSERT |
| 72 | + Assert.Equal(anonymous.Foo, actual.Foo); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void InvokeProcessWhenMethodHasZeroParametersDoesntParseMessage() |
| 77 | + { |
| 78 | + // ARRANGE |
| 79 | + var mi = typeof(MethodsCollection).GetMethod(nameof(MethodsCollection.ConstantMethod)); |
| 80 | + |
| 81 | + // ACT1 |
| 82 | + var actual = (string)LightWorker.InvokeProcess(mi, null); |
| 83 | + |
| 84 | + // ASSERT |
| 85 | + Assert.Equal(MethodsCollection.Value, actual); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void InvokeProcessWhenMethodHasAuthorizeAndMessageHasNoContextThrows() |
| 90 | + { |
| 91 | + // ARRANGE |
| 92 | + var mi = typeof(MethodsCollection).GetMethod(nameof(MethodsCollection.AuthorizedRoleCustomer)); |
| 93 | + var data = ToJsonByteStream(new Foobar { Context = null }); |
| 94 | + |
| 95 | + // ACT |
| 96 | + Assert.ThrowsAny<LightException>(() => LightWorker.InvokeProcess(mi, data)); |
| 97 | + } |
| 98 | + |
| 99 | + [Fact] |
| 100 | + public void InvokeProcessWhenMethodHasAuthorizeAndContextDoesntHaveAUserThrows() |
| 101 | + { |
| 102 | + // ARRANGE |
| 103 | + var mi = typeof(MethodsCollection).GetMethod(nameof(MethodsCollection.AuthorizedRoleCustomer)); |
| 104 | + var data = ToJsonByteStream(new Foobar()); |
| 105 | + |
| 106 | + // ACT & ASSERT |
| 107 | + Assert.ThrowsAny<LightException>(() => LightWorker.InvokeProcess(mi, data)); |
| 108 | + } |
| 109 | + |
| 110 | + [Fact] |
| 111 | + public void InvokeProcessWhenMethodHasAuthorizeAndUserDoesntHaveRoleThrows() |
| 112 | + { |
| 113 | + // ARRANGE |
| 114 | + var mi = typeof(MethodsCollection).GetMethod(nameof(MethodsCollection.AuthorizedRoleCustomer)); |
| 115 | + var data = ToJsonByteStream(new Foobar()); |
| 116 | + |
| 117 | + // ACT & ASSERT |
| 118 | + Assert.ThrowsAny<LightException>(() => LightWorker.InvokeProcess(mi, data)); |
| 119 | + } |
| 120 | + |
| 121 | + [Fact] |
| 122 | + public void InvokeProcessWhenMethodHasAuthorizeAndUserDoesntHaveRoleReturnsExpected() |
| 123 | + { |
| 124 | + // ARRANGE |
| 125 | + var mi = typeof(MethodsCollection).GetMethod(nameof(MethodsCollection.AuthorizedRoleAdmin)); |
| 126 | + var obj = new Foobar(); |
| 127 | + |
| 128 | + var claims = new[] { new Claim(ClaimTypes.Role, "admin") }; |
| 129 | + var key = HMAC.Create("HMACSHA1"); |
| 130 | + var signingCredentials = new SigningCredentials(new SymmetricSecurityKey(key.Key), "HMACSHA1"); |
| 131 | + var jwt = new JwtSecurityToken(claims: claims, signingCredentials: signingCredentials); |
| 132 | + var headerAndPayload = $"{jwt.EncodedHeader}.{jwt.EncodedPayload}"; |
| 133 | + var signature = Base64UrlEncoder.Encode(key.ComputeHash(Encoding.UTF8.GetBytes(headerAndPayload))); |
| 134 | + obj.TokenJwt = $"{headerAndPayload}.{signature}"; |
| 135 | + |
| 136 | + var data = ToJsonByteStream(obj); |
| 137 | + |
| 138 | + // ACT |
| 139 | + var actual = LightWorker.InvokeProcess(mi, data); |
| 140 | + |
| 141 | + // ASSERT |
| 142 | + Assert.Equal("Bar", actual); |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Serialize any object to a JSON string and then convert it to a bytestream. |
| 147 | + /// </summary> |
| 148 | + /// <param name="obj">The object to serialize.</param> |
| 149 | + /// <returns>A byestream containing the object as UTF8 bytes.</returns> |
| 150 | + private byte[] ToJsonByteStream(object obj) |
| 151 | + { |
| 152 | + var anonymousAsString = JsonConvert.SerializeObject(obj); |
| 153 | + var anonymousAsByteStream = Encoding.UTF8.GetBytes(anonymousAsString); |
| 154 | + |
| 155 | + return anonymousAsByteStream; |
| 156 | + } |
| 157 | + |
| 158 | + [SuppressMessage( |
| 159 | + "Design", |
| 160 | + "CA1034:Nested types should not be visible", |
| 161 | + Justification = "Must be public so LightWorker access the class")] |
| 162 | + public class Foobar : LightMessage<Foobar> |
| 163 | + { |
| 164 | + public string Foo { get; set; } = "Bar"; |
| 165 | + |
| 166 | + public override void Validate() |
| 167 | + { |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private class MethodsCollection |
| 172 | + { |
| 173 | + public const string Value = "string"; |
| 174 | + |
| 175 | + public string ConstantMethod() |
| 176 | + { |
| 177 | + return Value; |
| 178 | + } |
| 179 | + |
| 180 | + public Foobar FoobarMethod(Foobar foobar) |
| 181 | + { |
| 182 | + return foobar; |
| 183 | + } |
| 184 | + |
| 185 | + [Authorize(Roles = "customer")] |
| 186 | + public string AuthorizedRoleCustomer(Foobar value) |
| 187 | + { |
| 188 | + return value.Foo; |
| 189 | + } |
| 190 | + |
| 191 | + [Authorize(Roles = "admin")] |
| 192 | + public string AuthorizedRoleAdmin(Foobar value) |
| 193 | + { |
| 194 | + return value.Foo; |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments