示例#1
0
        public static async Task SetDefaultValueDbContextAsync()
        {
            using BotDbContext db = CreateMemoryDbContext();

            db.TelegramUsers.Add(new TelegramUser {
                Id = 123456789, UserName = "******"
            });
            db.TelegramUsers.Add(new TelegramUser {
                Id = 987654321, UserName = "******"
            });
            Guid clientId1 = Guid.NewGuid();
            Guid clientId2 = Guid.NewGuid();
            Guid clientId3 = Guid.NewGuid();

            db.AzureApps.Add(new AzureApp {
                Id = clientId1, Email = "*****@*****.**", Secrets = string.Empty, TelegramUserId = 123456789, Name = "App1"
            });
            db.AzureApps.Add(new AzureApp {
                Id = clientId2, Email = "*****@*****.**", Secrets = string.Empty, TelegramUserId = 123456789, Name = "App2"
            });
            db.AzureApps.Add(new AzureApp {
                Id = clientId3, Email = "*****@*****.**", Secrets = string.Empty, TelegramUserId = 987654321, Name = "App3"
            });
            db.AppAuths.Add(new AppAuth {
                Name = "Auth1", RefreshToken = string.Empty, Scope = DefaultGraphApi.Scope, AzureAppId = clientId1
            });
            db.AppAuths.Add(new AppAuth {
                Name = "Auth3", RefreshToken = string.Empty, Scope = "", AzureAppId = clientId3
            });

            await db.SaveChangesAsync();

            await db.DisposeAsync();
        }
示例#2
0
        public async Task TestRegAppAsync()
        {
            string          testResultPath  = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ApiResults", "ValidApplicationResult.json");
            string          json            = File.ReadAllText(testResultPath);
            var             mocks           = Utils.CreateDefaultGraphApiMock(json);
            BotDbContext    db              = Utils.CreateMemoryDbContext();
            DefaultGraphApi defaultGraphApi = new DefaultGraphApi(db, mocks.Item1, mocks.Item2);

            BindHandler bindHandler = new BindHandler(db, defaultGraphApi);

            long   userId       = 123456;
            string userName     = "******";
            string email        = "*****@*****.**";
            Guid   clientId     = Guid.NewGuid();
            string clientSecret = "741852963";
            string appName      = "app1";

            await bindHandler.RegAppAsync(userId, userName, email, clientId.ToString(), clientSecret, appName);

            await db.DisposeAsync();

            db = Utils.CreateMemoryDbContext();
            AzureApp azureApp = await db.AzureApps.Include(azureApp => azureApp.TelegramUser).FirstAsync();

            Assert.AreEqual(userId, azureApp.TelegramUserId);
            Assert.AreEqual(userName, azureApp.TelegramUser.UserName);
            Assert.AreEqual(email, azureApp.Email);
            Assert.AreEqual(clientId, azureApp.Id);
            Assert.AreEqual(clientSecret, azureApp.Secrets);
            Assert.AreEqual(appName, azureApp.Name);
        }
示例#3
0
        public static async Task SetApiResultDbContextAsync()
        {
            using BotDbContext db = CreateMemoryDbContext();

            db.TelegramUsers.Add(new TelegramUser {
                Id = 123456789, UserName = "******"
            });
            db.TelegramUsers.Add(new TelegramUser {
                Id = 987654321, UserName = "******"
            });

            db.ApiResults.Add(new ApiResult {
                Result = "123", TelegramUserId = 123456789
            });
            db.ApiResults.Add(new ApiResult {
                Result = "123987456", TelegramUserId = 123456789
            });
            db.ApiResults.Add(new ApiResult {
                Result = "987654321", TelegramUserId = 987654321
            });

            await db.SaveChangesAsync();

            await db.DisposeAsync();
        }
示例#4
0
        public async Task TestUpdateAuthAsync()
        {
            string token = await Utils.GetTestToken();

            string  authResponsePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ApiResults", "ValidAuthResponse.json");
            string  authResponse     = File.ReadAllText(authResponsePath);
            string  testResultPath   = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ApiResults", "GetTokenSuccessResult.json");
            string  json             = File.ReadAllText(testResultPath);
            JObject jObject          = JObject.Parse(json);

            jObject["access_token"] = token;
            json = JsonConvert.SerializeObject(jObject);
            Guid clientId = Guid.NewGuid();
            var  mocks    = Utils.CreateDefaultGraphApiMock(json);
            await Utils.SetOneValueDbContextAsync(clientId);

            BotDbContext    db = Utils.CreateMemoryDbContext();
            DefaultGraphApi defaultGraphApi = new DefaultGraphApi(db, mocks.Item1, mocks.Item2);
            Guid            authId          = (await db.AppAuths.AsQueryable().FirstAsync(item => item.AzureAppId == clientId)).Id;

            BindHandler bindHandler = new BindHandler(db, defaultGraphApi);
            await bindHandler.UpdateAuthAsync(authId.ToString(), authResponse);

            await db.DisposeAsync();

            db = Utils.CreateMemoryDbContext();
            Assert.AreEqual(1, await db.AppAuths.AsQueryable().CountAsync());
            Assert.IsTrue(await db.AppAuths.AsQueryable().AnyAsync(appAuth => appAuth.Id == authId && appAuth.RefreshToken == jObject["refresh_token"].ToString()));
        }
示例#5
0
        public async Task TestUnbindNotExistAuthAsync()
        {
            await Utils.SetDefaultValueDbContextAsync();

            BotDbContext db      = Utils.CreateMemoryDbContext();
            Guid         authId1 = Guid.NewGuid();
            await db.DisposeAsync();

            db = Utils.CreateMemoryDbContext();

            BindHandler bindHandler = new BindHandler(db, null);
            await bindHandler.UnbindAuthAsync(authId1.ToString());
        }
示例#6
0
        public async Task TestDeleteNotExistAppAsync()
        {
            await Utils.SetDefaultValueDbContextAsync();

            BotDbContext db        = Utils.CreateMemoryDbContext();
            Guid         clientId1 = Guid.NewGuid();
            await db.DisposeAsync();

            db = Utils.CreateMemoryDbContext();

            BindHandler bindHandler = new BindHandler(db, null);
            string      _           = await bindHandler.DeleteAppAsync(clientId1.ToString());
        }
示例#7
0
        public async Task TestAddAdminPermissionFailed()
        {
            BotDbContext db = Utils.CreateMemoryDbContext();

            TelegramHandler telegramHandler = new TelegramHandler(logger, configuration, db);
            bool            result          = await telegramHandler.AddAdminPermissionAsync(123456789, "Bot1", "@@@@@@@");

            await db.DisposeAsync();

            db = Utils.CreateMemoryDbContext();

            Assert.IsFalse(result);
            Assert.AreEqual(0, await db.TelegramUsers.AsQueryable().CountAsync());
        }
示例#8
0
        public async Task TestRemoveAdminPermissionNoUser()
        {
            BotDbContext db = Utils.CreateMemoryDbContext();

            TelegramHandler telegramHandler = new TelegramHandler(logger, configuration, db);
            await telegramHandler.RemoveAdminPermissionAsync(123456789, "Bot1");

            await db.DisposeAsync();

            db = Utils.CreateMemoryDbContext();
            TelegramUser user = await db.TelegramUsers.FindAsync((long)123456789);

            Assert.AreEqual(1, await db.TelegramUsers.AsQueryable().CountAsync());
            Assert.AreEqual("Bot1", user.UserName);
            Assert.IsFalse(user.IsAdmin);
        }
示例#9
0
        public async Task TestUnbindAuthAsync()
        {
            await Utils.SetDefaultValueDbContextAsync();

            BotDbContext db      = Utils.CreateMemoryDbContext();
            Guid         authId1 = await db.AppAuths.AsQueryable().Select(app => app.Id).FirstAsync();

            await db.DisposeAsync();

            db = Utils.CreateMemoryDbContext();

            BindHandler bindHandler = new BindHandler(db, null);
            await bindHandler.UnbindAuthAsync(authId1.ToString());

            Assert.AreEqual(await db.AppAuths.AsQueryable().CountAsync(), 1);
        }
示例#10
0
        public async Task TestAddAdminPermissionSuccessNoUser()
        {
            BotDbContext db = Utils.CreateMemoryDbContext();

            TelegramHandler telegramHandler = new TelegramHandler(logger, configuration, db);
            bool            result          = await telegramHandler.AddAdminPermissionAsync(123456789, "Bot1", "P@ssw0rd");

            await db.DisposeAsync();

            db = Utils.CreateMemoryDbContext();
            TelegramUser user = await db.TelegramUsers.FindAsync((long)123456789);

            Assert.IsTrue(result);
            Assert.AreEqual(1, await db.TelegramUsers.AsQueryable().CountAsync());
            Assert.AreEqual("Bot1", user.UserName);
            Assert.IsTrue(user.IsAdmin);
        }
示例#11
0
        public async Task TestCheckIsAdminTrue()
        {
            await Utils.SetDefaultValueDbContextAsync();

            BotDbContext db = Utils.CreateMemoryDbContext();

            TelegramHandler telegramHandler = new TelegramHandler(logger, configuration, db);
            await telegramHandler.AddAdminPermissionAsync(123456789, "Bot1", "P@ssw0rd");

            await db.DisposeAsync();

            db = Utils.CreateMemoryDbContext();
            telegramHandler = new TelegramHandler(logger, configuration, db);
            bool result = await telegramHandler.CheckIsAdminAsync(123456789);

            Assert.IsTrue(result);
        }
示例#12
0
        public async Task TestDeleteAppAsync()
        {
            await Utils.SetDefaultValueDbContextAsync();

            BotDbContext db        = Utils.CreateMemoryDbContext();
            Guid         clientId1 = await db.AzureApps.AsQueryable().Select(app => app.Id).FirstAsync();

            await db.DisposeAsync();

            db = Utils.CreateMemoryDbContext();

            BindHandler bindHandler = new BindHandler(db, null);
            string      deleteUrl   = await bindHandler.DeleteAppAsync(clientId1.ToString());

            Assert.AreEqual(deleteUrl, string.Format(BindHandler.DeleteUrl, clientId1.ToString()));
            Assert.AreEqual(await db.AzureApps.AsQueryable().CountAsync(), 2);
            Assert.AreEqual(await db.AppAuths.AsQueryable().CountAsync(), 1);
        }
示例#13
0
        public async Task TestGetAuthInfoAsync()
        {
            await Utils.SetDefaultValueDbContextAsync();

            BotDbContext db      = Utils.CreateMemoryDbContext();
            Guid         authId1 = await db.AppAuths.AsQueryable().Select(app => app.Id).FirstAsync();

            await db.DisposeAsync();

            db = Utils.CreateMemoryDbContext();

            TelegramHandler telegramHandler = new TelegramHandler(logger, configuration, db);
            var             authInfo        = (await telegramHandler.GetAuthInfoAsync(authId1.ToString()));

            Assert.AreEqual(authInfo.Name, "Auth1");
            Assert.AreEqual(authInfo.RefreshToken, string.Empty);
            Assert.AreEqual(authInfo.Scope, DefaultGraphApi.Scope);
            Assert.AreEqual(authInfo.Id, authId1);
        }
示例#14
0
        public async Task TestGetAppInfoAsync()
        {
            await Utils.SetDefaultValueDbContextAsync();

            BotDbContext db        = Utils.CreateMemoryDbContext();
            Guid         clientId1 = await db.AzureApps.AsQueryable().Select(app => app.Id).FirstAsync();

            await db.DisposeAsync();

            db = Utils.CreateMemoryDbContext();

            TelegramHandler telegramHandler = new TelegramHandler(logger, configuration, db);
            var             appInfo         = (await telegramHandler.GetAppInfoAsync(clientId1.ToString()));

            Assert.AreEqual(appInfo.Name, "App1");
            Assert.AreEqual(appInfo.Email, "*****@*****.**");
            Assert.AreEqual(appInfo.Secrets, string.Empty);
            Assert.AreEqual(appInfo.TelegramUserId, 123456789);
            Assert.AreEqual(appInfo.Id, clientId1);
        }