Esempio n. 1
0
        public async Task setting_default_scopes_impact_new_users()
        {
            var user    = TestHelper.StObjMap.StObjs.Obtain <UserTable>();
            var p       = TestHelper.StObjMap.StObjs.Obtain <Package>();
            var factory = TestHelper.StObjMap.StObjs.Obtain <IPocoFactory <IUserTwitterInfo> >();

            using (var ctx = new SqlStandardCallContext())
            {
                AuthScopeSet original = await p.ReadDefaultScopeSetAsync(ctx);

                original.Contains("nimp").Should().BeFalse();
                original.Contains("thing").Should().BeFalse();
                original.Contains("other").Should().BeFalse();

                {
                    int id = await user.CreateUserAsync(ctx, 1, Guid.NewGuid().ToString());

                    IUserTwitterInfo userInfo = factory.Create();
                    userInfo.TwitterAccountId = Guid.NewGuid().ToString();
                    await p.UserTwitterTable.CreateOrUpdateTwitterUserAsync(ctx, 1, id, userInfo);

                    var info = await p.UserTwitterTable.FindKnownUserInfoAsync(ctx, userInfo.TwitterAccountId);

                    AuthScopeSet userSet = await p.ReadScopeSetAsync(ctx, info.UserId);

                    userSet.ToString().Should().Be(original.ToString());
                }
                AuthScopeSet replaced = original.Clone();
                replaced.Add(new AuthScopeItem("nimp"));
                replaced.Add(new AuthScopeItem("thing", ScopeWARStatus.Rejected));
                replaced.Add(new AuthScopeItem("other", ScopeWARStatus.Accepted));
                await p.AuthScopeSetTable.SetScopesAsync(ctx, 1, replaced);

                var readback = await p.ReadDefaultScopeSetAsync(ctx);

                readback.ToString().Should().Be(replaced.ToString());
                // Default scopes have non W status!
                // This must not impact new users: their satus must always be be W.
                readback.ToString().Should().Contain("[R]thing")
                .And.Contain("[A]other");

                {
                    int id = await user.CreateUserAsync(ctx, 1, Guid.NewGuid().ToString());

                    IUserTwitterInfo userInfo = p.UserTwitterTable.CreateUserInfo <IUserTwitterInfo>();
                    userInfo.TwitterAccountId = Guid.NewGuid().ToString();
                    await p.UserTwitterTable.CreateOrUpdateTwitterUserAsync(ctx, 1, id, userInfo, UCLMode.CreateOnly | UCLMode.UpdateOnly);

                    userInfo = (IUserTwitterInfo)(await p.UserTwitterTable.FindKnownUserInfoAsync(ctx, userInfo.TwitterAccountId)).Info;
                    AuthScopeSet userSet = await p.ReadScopeSetAsync(ctx, id);

                    userSet.ToString().Should().Contain("[W]thing")
                    .And.Contain("[W]other")
                    .And.Contain("[W]nimp");
                }
                await p.AuthScopeSetTable.SetScopesAsync(ctx, 1, original);
            }
        }
Esempio n. 2
0
        public async Task AuthScopeSet_manipulation()
        {
            var scopes = TestHelper.StObjMap.Default.Obtain <AuthScopeSetTable>();

            using (var ctx = new SqlStandardCallContext())
            {
                var set = new AuthScopeSet(new[] {
                    new AuthScopeItem("A", ScopeWARStatus.Accepted),
                    new AuthScopeItem("B", ScopeWARStatus.Waiting),
                    new AuthScopeItem("C", ScopeWARStatus.Rejected)
                });

                var id = await scopes.CreateScopeSetAsync(ctx, 1, set.Scopes);

                scopes.Database.ExecuteScalar($"select ScopesWithStatus from CK.vAuthScopeSet where ScopeSetId = {id}")
                .Should().Be("[A]A [W]B [R]C");

                var readSet = await scopes.ReadAuthScopeSetAsync(ctx, id);

                readSet.ScopeSetId.Should().Be(id);
                readSet.ToString().Should().Be("[A]A [W]B [R]C");

                set.ScopeSetId = id;
                set.Add(new AuthScopeItem("B", ScopeWARStatus.Accepted));
                set.Add(new AuthScopeItem("D", ScopeWARStatus.Waiting));
                await scopes.AddOrUpdateScopesAsync(ctx, 1, set);

                readSet = await scopes.ReadAuthScopeSetAsync(ctx, id);

                readSet.ToString().Should().Be("[A]A [A]B [R]C [W]D");

                set.Remove("B");
                set.Remove("C");
                set.Add(new AuthScopeItem("E", ScopeWARStatus.Accepted));
                await scopes.AddOrUpdateScopesAsync(ctx, 1, set);

                readSet = await scopes.ReadAuthScopeSetAsync(ctx, id);

                readSet.ToString().Should().Be("[A]A [A]B [R]C [W]D [A]E");

                set.Remove("E");
                await scopes.SetScopesAsync(ctx, 1, set);

                readSet = await scopes.ReadAuthScopeSetAsync(ctx, id);

                readSet.ToString().Should().Be("[A]A [W]D");

                await scopes.DestroyScopeSetAsync(ctx, 1, id);
            }
        }