Beispiel #1
0
    public async Task ReusesCachedProtectorsByPurpose()
    {
        // Arrange
        var jsRuntime = new TestJSRuntime();

        jsRuntime.NextInvocationResult = new ValueTask <IJSVoidResult>(Mock.Of <IJSVoidResult>());
        var dataProtectionProvider  = new TestDataProtectionProvider();
        var protectedBrowserStorage = new TestProtectedBrowserStorage("testStore", jsRuntime, dataProtectionProvider);

        // Act
        await protectedBrowserStorage.SetAsync("key 1", null);

        await protectedBrowserStorage.SetAsync("key 2", null);

        await protectedBrowserStorage.SetAsync("key 1", null);

        await protectedBrowserStorage.SetAsync("key 3", null);

        // Assert
        var typeName         = typeof(TestProtectedBrowserStorage).FullName;
        var expectedPurposes = new[]
        {
            $"{typeName}:testStore:key 1",
            $"{typeName}:testStore:key 2",
            $"{typeName}:testStore:key 3"
        };

        Assert.Equal(expectedPurposes, dataProtectionProvider.ProtectorsCreated.ToArray());

        Assert.Collection(jsRuntime.Invocations,
                          invocation => Assert.Equal(TestDataProtectionProvider.Protect(expectedPurposes[0], "null"), invocation.Args[1]),
                          invocation => Assert.Equal(TestDataProtectionProvider.Protect(expectedPurposes[1], "null"), invocation.Args[1]),
                          invocation => Assert.Equal(TestDataProtectionProvider.Protect(expectedPurposes[0], "null"), invocation.Args[1]),
                          invocation => Assert.Equal(TestDataProtectionProvider.Protect(expectedPurposes[2], "null"), invocation.Args[1]));
    }
        public void SetAsync_ProtectsAndInvokesJS(string customPurpose)
        {
            // Arrange
            var jsRuntime = new TestJSRuntime();
            var dataProtectionProvider  = new TestDataProtectionProvider();
            var protectedBrowserStorage = new TestProtectedBrowserStorage("test store", jsRuntime, dataProtectionProvider);
            var jsResultTask            = Task.FromResult((object)null);
            var data = new TestModel {
                StringProperty = "Hello", IntProperty = 123
            };
            var keyName         = "test key";
            var expectedPurpose = customPurpose == null
                ? $"{typeof(TestProtectedBrowserStorage).FullName}:test store:{keyName}"
                : customPurpose;

            // Act
            jsRuntime.NextInvocationResult = jsResultTask;
            var result = customPurpose == null
                ? protectedBrowserStorage.SetAsync(keyName, data)
                : protectedBrowserStorage.SetAsync(customPurpose, keyName, data);

            // Assert
            Assert.Same(jsResultTask, result);
            var invocation = jsRuntime.Invocations.Single();

            Assert.Equal("protectedBrowserStorage.set", invocation.Identifier);
            Assert.Collection(invocation.Args,
                              arg => Assert.Equal("test store", arg),
                              arg => Assert.Equal(keyName, arg),
                              arg => Assert.Equal(
                                  "{\"StringProperty\":\"Hello\",\"IntProperty\":123}",
                                  TestDataProtectionProvider.Unprotect(expectedPurpose, (string)arg)));
        }
Beispiel #3
0
    public void SetAsync_ProtectsAndInvokesJS_CustomPurpose()
    {
        // Arrange
        var jsRuntime = new TestJSRuntime();
        var dataProtectionProvider  = new TestDataProtectionProvider();
        var protectedBrowserStorage = new TestProtectedBrowserStorage("testStore", jsRuntime, dataProtectionProvider);
        var jsResultTask            = new ValueTask <object>((object)null);
        var data = new TestModel {
            StringProperty = "Hello", IntProperty = 123
        };
        var keyName       = "testKey";
        var customPurpose = "my custom purpose";

        // Act
        jsRuntime.NextInvocationResult = jsResultTask;
        var result = protectedBrowserStorage.SetAsync(customPurpose, keyName, data);

        // Assert
        var invocation = jsRuntime.Invocations.Single();

        Assert.Equal("testStore.setItem", invocation.Identifier);
        Assert.Collection(invocation.Args,
                          arg => Assert.Equal(keyName, arg),
                          arg => Assert.Equal(
                              "{\"stringProperty\":\"Hello\",\"intProperty\":123}",
                              TestDataProtectionProvider.Unprotect(customPurpose, (string)arg)));
    }
        public void SetAsync_ProtectsAndInvokesJS_NullValue()
        {
            // Arrange
            var jsRuntime = new TestJSRuntime();
            var dataProtectionProvider  = new TestDataProtectionProvider();
            var protectedBrowserStorage = new TestProtectedBrowserStorage("test store", jsRuntime, dataProtectionProvider);
            var jsResultTask            = Task.FromResult((object)null);
            var expectedPurpose         = $"{typeof(TestProtectedBrowserStorage).FullName}:test store:test key";

            // Act
            jsRuntime.NextInvocationResult = jsResultTask;
            var result = protectedBrowserStorage.SetAsync("test key", null);

            // Assert
            Assert.Same(jsResultTask, result);
            var invocation = jsRuntime.Invocations.Single();

            Assert.Equal("protectedBrowserStorage.set", invocation.Identifier);
            Assert.Collection(invocation.Args,
                              arg => Assert.Equal("test store", arg),
                              arg => Assert.Equal("test key", arg),
                              arg => Assert.Equal(
                                  "null",
                                  TestDataProtectionProvider.Unprotect(expectedPurpose, (string)arg)));
        }