Exemplo n.º 1
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);
        }
Exemplo n.º 2
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()));
        }
Exemplo n.º 3
0
        public async Task TestGetAuthUrlAsyncEmailNotExist()
        {
            Guid            clientId        = Guid.NewGuid();
            var             mocks           = Utils.CreateDefaultGraphApiMock(string.Empty);
            BotDbContext    db              = Utils.CreateMemoryDbContext();
            DefaultGraphApi defaultGraphApi = new DefaultGraphApi(db, mocks.Item1, mocks.Item2);

            BindHandler bindHandler = new BindHandler(db, defaultGraphApi);

            (string, string)_ = await bindHandler.GetAuthUrlAsync(clientId.ToString());
        }
Exemplo n.º 4
0
        public async Task TestGetTokenAzureAppNotFoundAsync()
        {
            string       testResultPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ApiResults", "GetTokenSuccessResult.json");
            string       json           = File.ReadAllText(testResultPath);
            Guid         clientId       = Guid.NewGuid();
            var          mocks          = Utils.CreateDefaultGraphApiMock(json);
            BotDbContext db             = Utils.CreateMemoryDbContext();

            DefaultGraphApi defaultGraphApi = new DefaultGraphApi(db, mocks.Item1, mocks.Item2);

            (string, string)_ = await defaultGraphApi.GetTokenAsync(clientId, string.Empty);
        }
        public ApiControllerTests()
        {
            string token = Utils.GetTestToken().Result;

            graphClient = DefaultGraphApi.GetGraphServiceClient(token);
            services    = new ServiceCollection();
            services.AddLogging();
            services.AddScoped <GraphApi, OutlookApi>();
            services.AddScoped <GraphApi, OneDriveApi>();
            services.AddScoped <GraphApi, PermissionsApi>();
            services.AddScoped <GraphApi, CalendarApi>();
            services.AddScoped <GraphApi, PersonalContactsApi>();
            services.AddScoped <ApiController>();
        }
Exemplo n.º 6
0
        public async Task TestIsInvalidApplicationAsync()
        {
            string testResultPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ApiResults", "InvalidApplicationResult.json");
            string json           = File.ReadAllText(testResultPath);
            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);

            Assert.IsFalse(await defaultGraphApi.IsValidApplicationAsync("*****@*****.**", string.Empty, string.Empty));
        }
Exemplo n.º 7
0
        public async Task TestReflashTokenFailedAsync()
        {
            string testResultPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ApiResults", "GetTokenFailedResult.json");
            string json           = File.ReadAllText(testResultPath);
            Guid   clientId       = Guid.NewGuid();
            var    mocks          = Utils.CreateDefaultGraphApiMock(json);
            await Utils.SetOneValueDbContextAsync(clientId);

            BotDbContext db      = Utils.CreateMemoryDbContext();
            AppAuth      appAuth = await db.AppAuths.Include(appAuth => appAuth.AzureApp).FirstAsync();

            DefaultGraphApi defaultGraphApi = new DefaultGraphApi(db, mocks.Item1, mocks.Item2);

            (string, string)_ = await defaultGraphApi.ReflashTokenAsync(appAuth);
        }
Exemplo n.º 8
0
        public async Task TestRegInvalidAppAsync()
        {
            string          testResultPath  = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ApiResults", "InvalidApplicationResult.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);
        }
Exemplo n.º 9
0
        public async Task TestGetTokenSuccessAsync()
        {
            string testResultPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ApiResults", "GetTokenSuccessResult.json");
            string json           = File.ReadAllText(testResultPath);
            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);

            (string, string)tokens = await defaultGraphApi.GetTokenAsync(clientId, string.Empty);

            JObject jObject = JObject.Parse(json);

            Assert.AreEqual(jObject["access_token"].ToString(), tokens.Item1);
            Assert.AreEqual(jObject["refresh_token"].ToString(), tokens.Item2);
        }
Exemplo n.º 10
0
        public async Task TestGetAuthUrlAsync()
        {
            Guid clientId = Guid.NewGuid();
            var  mocks    = Utils.CreateDefaultGraphApiMock(string.Empty);
            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);

            (string, string)results = await bindHandler.GetAuthUrlAsync(clientId.ToString());

            string email   = "*****@*****.**";
            string message = $"https://login.microsoftonline.com/{DefaultGraphApi.GetTenant(email)}/oauth2/v2.0/authorize?client_id={clientId}&response_type=code&redirect_uri={HttpUtility.UrlEncode(BindHandler.AppUrl)}&response_mode=query&scope={HttpUtility.UrlEncode(DefaultGraphApi.Scope)}";

            Assert.AreEqual(message, results.Item2);

            results = await bindHandler.GetAuthUrlAsync(authId.ToString(), false);

            Assert.AreEqual(message, results.Item2);
        }
Exemplo n.º 11
0
        public PersonalContactsApiTests()
        {
            string token = Utils.GetTestToken().Result;

            graphClient = DefaultGraphApi.GetGraphServiceClient(token);
        }
Exemplo n.º 12
0
        public CalendarApiTests()
        {
            string token = Utils.GetTestToken().Result;

            graphClient = DefaultGraphApi.GetGraphServiceClient(token);
        }
Exemplo n.º 13
0
 public async Task TeskGetUserInfoFailedAsync()
 {
     _ = await DefaultGraphApi.GetUserInfoAsync(string.Empty);
 }
Exemplo n.º 14
0
        public async Task TeskGetUserInfoSuccessAsync()
        {
            string token = await Utils.GetTestToken();

            Assert.IsNotNull(await DefaultGraphApi.GetUserInfoAsync(token));
        }