public void decline_expired_pending_status_should_redirect(string status)
        {
            var testUser = new AppUser(new AspNetUser()
            {
                Id                 = Guid.NewGuid().ToString(),
                BillingOn          = DateTime.Now,
                MyShopifyDomain    = "store1.myshopify.com",
                ShopifyChargeId    = 1,
                Email              = "*****@*****.**",
                PlanId             = 1,
                UserName           = "******",
                ShopifyAccessToken = "thetoken"
            }, false);

            //moq ishopifyapi
            var shopifyApi = new Mock <IShopifyApi>();

            shopifyApi.Setup(x => x.GetRecurringChargeAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <long>(), It.IsAny <string>()))
            .Returns(Task.FromResult(new ShopifyRecurringChargeObject()
            {
                Status = status
            }));

            //mock user caching
            var userCache = new Mock <IUserCaching>();

            userCache.Setup(x => x.GetLoggedOnUser(It.IsAny <bool>())).Returns(Task.FromResult(testUser));
            userCache.Setup(x => x.ClearLoggedOnUser());
            //db service mock
            var userdb = new Mock <IDbService <AspNetUser> >();

            userdb.Setup(x => x.FindSingleWhere(It.IsAny <Expression <Func <AspNetUser, bool> > >()))
            .Returns(() => new AspNetUser()
            {
                Id = testUser.Id
            });
            userdb.Setup(x => x.Update(It.IsAny <AspNetUser>(), It.IsAny <object>())).Returns(() => It.IsAny <AspNetUser>());

            RequireSubscription obj = new RequireSubscription(shopifyApi.Object, realSettingsReader, emailer.Object, userCache.Object, userdb.Object, mockLogger.Object);
            var authCtx             = _GetMockAuthrizationContext();

            obj.OnAuthorization(authCtx);
            //check that we are clearing log
            userCache.Verify(x => x.ClearLoggedOnUser(), Times.Exactly(1));
            //check that we are finding that user and  unsetting (updating) billing information
            userdb.VerifyAll();
            //check shopify api was called as well to check recurring charge status
            shopifyApi.VerifyAll();
            //now check result
            var result = authCtx.Result;

            Assert.IsType <RedirectToRouteResult>(result);
            var routeResult = (RedirectToRouteResult)result;

            Assert.NotNull(routeResult.RouteValues["controller"]);
            Assert.NotNull(routeResult.RouteValues["action"]);
            Assert.Equal(routeResult.RouteValues["controller"], realSettingsReader.GetShopifyControllerName());
            Assert.Equal(routeResult.RouteValues["action"], SHOPIFY_ACTIONS.HandShake.ToString());
        }
        public void admin_by_passes_disconnected_or_connected_billing(bool billingDiconnected)
        {
            long?chargeid = 999;
            var  testUser = new AppUser(new AspNetUser()
            {
                Id = Guid.NewGuid().ToString(),
                ShopifyChargeId    = billingDiconnected == true ?  chargeid : null,
                BillingOn          = DateTime.Now,
                MyShopifyDomain    = "store1.myshopify.com",
                Email              = "*****@*****.**",
                PlanId             = 1,
                UserName           = "******",
                ShopifyAccessToken = "asdasdasd"
            }, true);


            //moq ishopifyapi
            var shopifyApi = new Mock <IShopifyApi>();

            shopifyApi.Setup(x => x.GetRecurringChargeAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <long>(), It.IsAny <string>()))
            .Returns(Task.FromResult(new ShopifyRecurringChargeObject()
            {
                Status = SHOPIFY_CHARGE_STATUS.accepted.ToString()
            }));

            //mock user caching
            var userCache = new Mock <IUserCaching>();

            userCache.Setup(x => x.GetLoggedOnUser(It.IsAny <bool>())).Returns(Task.FromResult(testUser));
            userCache.Setup(x => x.ClearLoggedOnUser());
            //db service mock
            var userdb = new Mock <IDbService <AspNetUser> >();

            userdb.Setup(x => x.FindSingleWhere(It.IsAny <Expression <Func <AspNetUser, bool> > >()))
            .Returns(() => new AspNetUser()
            {
                Id = testUser.Id
            });
            userdb.Setup(x => x.Update(It.IsAny <AspNetUser>(), It.IsAny <object>())).Returns(() => It.IsAny <AspNetUser>());

            RequireSubscription obj = new RequireSubscription(shopifyApi.Object, realSettingsReader, emailer.Object, userCache.Object, userdb.Object, mockLogger.Object);
            var authCtx             = _GetMockAuthrizationContext();

            obj.OnAuthorization(authCtx);
            var result = authCtx.Result;

            //shopify api is NEVER called to check admin's chage status
            shopifyApi.Verify(x => x.GetRecurringChargeAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <long>(), It.IsAny <string>()), Times.Never());
            userdb.Verify(x => x.Update(It.IsAny <AspNetUser>(), It.IsAny <string>()), Times.Never()); //nothing is saved in the db
            userCache.Verify(x => x.ClearLoggedOnUser(), Times.Never());                               //nothing is cleared from cache
            Assert.Null(result);                                                                       //means no redirect happened
        }
        public void disconnected_billing_should_redirect()
        {
            var testUser = new AppUser(new AspNetUser()
            {
                Id                 = Guid.NewGuid().ToString(),
                BillingOn          = DateTime.Now,
                MyShopifyDomain    = "store1.myshopify.com",
                ShopifyChargeId    = null,/*making diconnected*/
                Email              = "*****@*****.**",
                PlanId             = 1,
                UserName           = "******",
                ShopifyAccessToken = "asdasdasd"
            }, false);

            //moq ishopifyapi
            var shopifyApi = new Mock <IShopifyApi>();

            shopifyApi.Setup(x => x.GetRecurringChargeAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <long>(), It.IsAny <string>()))
            .Returns(Task.FromResult(new ShopifyRecurringChargeObject()
            {
                Status = SHOPIFY_CHARGE_STATUS.accepted.ToString()
            }));

            //mock user caching
            var userCache = new Mock <IUserCaching>();

            userCache.Setup(x => x.GetLoggedOnUser(It.IsAny <bool>())).Returns(Task.FromResult(testUser));
            userCache.Setup(x => x.ClearLoggedOnUser());
            //db service mock
            var userdb = new Mock <IDbService <AspNetUser> >();

            userdb.Setup(x => x.FindSingleWhere(It.IsAny <Expression <Func <AspNetUser, bool> > >()))
            .Returns(() => new AspNetUser()
            {
                Id = testUser.Id
            });
            userdb.Setup(x => x.Update(It.IsAny <AspNetUser>(), It.IsAny <object>())).Returns(() => It.IsAny <AspNetUser>());

            RequireSubscription obj = new RequireSubscription(shopifyApi.Object, realSettingsReader, emailer.Object, userCache.Object, userdb.Object, mockLogger.Object);
            var authCtx             = _GetMockAuthrizationContext();

            obj.OnAuthorization(authCtx);
            var result = authCtx.Result;

            Assert.IsType <RedirectToRouteResult>(result);
            var routeResult = (RedirectToRouteResult)result;

            Assert.NotNull(routeResult.RouteValues["controller"]);
            Assert.NotNull(routeResult.RouteValues["action"]);
            Assert.Equal(routeResult.RouteValues["controller"], realSettingsReader.GetShopifyControllerName());
            Assert.Equal(routeResult.RouteValues["action"], SHOPIFY_ACTIONS.ChoosePlan.ToString());
        }
        public void active_accepted_should_should_pass(string status)
        {
            var testUser = new AppUser(new AspNetUser()
            {
                Id                 = Guid.NewGuid().ToString(),
                BillingOn          = DateTime.Now,
                MyShopifyDomain    = "store1.myshopify.com",
                ShopifyChargeId    = 1,
                Email              = "*****@*****.**",
                PlanId             = 1,
                UserName           = "******",
                ShopifyAccessToken = "thetoken"
            }, false);

            //moq ishopifyapi
            var shopifyApi = new Mock <IShopifyApi>();

            shopifyApi.Setup(x => x.GetRecurringChargeAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <long>(), It.IsAny <string>()))
            .Returns(Task.FromResult(new ShopifyRecurringChargeObject()
            {
                Status = status
            }));

            //mock user caching
            var userCache = new Mock <IUserCaching>();

            userCache.Setup(x => x.GetLoggedOnUser(It.IsAny <bool>())).Returns(Task.FromResult(testUser));
            userCache.Setup(x => x.ClearLoggedOnUser());

            var userdb = new Mock <IDbService <AspNetUser> >();

            userdb.Setup(x => x.FindSingleWhere(It.IsAny <Expression <Func <AspNetUser, bool> > >()))
            .Returns(() => new AspNetUser()
            {
                Id = testUser.Id
            });
            userdb.Setup(x => x.Update(It.IsAny <AspNetUser>(), It.IsAny <object>())).Returns(() => It.IsAny <AspNetUser>());

            RequireSubscription obj = new RequireSubscription(shopifyApi.Object, realSettingsReader, emailer.Object, userCache.Object, userdb.Object, mockLogger.Object);
            var authCtx             = _GetMockAuthrizationContext();

            obj.OnAuthorization(authCtx);

            shopifyApi.VerifyAll();                                                                    //shopify api was called to check charge status
            userdb.Verify(x => x.Update(It.IsAny <AspNetUser>(), It.IsAny <string>()), Times.Never()); //nothing is saved in the db
            userCache.Verify(x => x.ClearLoggedOnUser(), Times.Never());                               //nothing is eared from cache
            Assert.Null(authCtx.Result);
        }
        public void frozen_charge_status_should_throw_exception()
        {
            var testUser = new AppUser(new AspNetUser()
            {
                Id                 = Guid.NewGuid().ToString(),
                BillingOn          = DateTime.Now,
                MyShopifyDomain    = "store1.myshopify.com",
                ShopifyChargeId    = 1,
                Email              = "*****@*****.**",
                PlanId             = 1,
                UserName           = "******",
                ShopifyAccessToken = "thetoken"
            }, false);

            //moq ishopifyapi
            var shopifyApi = new Mock <IShopifyApi>();

            shopifyApi.Setup(x => x.GetRecurringChargeAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <long>(), It.IsAny <string>()))
            .Returns(Task.FromResult(new ShopifyRecurringChargeObject()
            {
                Status = "frozen"
            }));

            //mock user caching
            var userCache = new Mock <IUserCaching>();

            userCache.Setup(x => x.GetLoggedOnUser(It.IsAny <bool>())).Returns(Task.FromResult(testUser));
            userCache.Setup(x => x.ClearLoggedOnUser());

            var userdb = new Mock <IDbService <AspNetUser> >();

            userdb.Setup(x => x.FindSingleWhere(It.IsAny <Expression <Func <AspNetUser, bool> > >()))
            .Returns(() => new AspNetUser()
            {
                Id = testUser.Id
            });
            userdb.Setup(x => x.Update(It.IsAny <AspNetUser>(), It.IsAny <object>())).Returns(() => It.IsAny <AspNetUser>());

            RequireSubscription obj = new RequireSubscription(shopifyApi.Object, realSettingsReader, emailer.Object, userCache.Object, userdb.Object, mockLogger.Object);

            Assert.ThrowsAny <Exception>(() => obj.OnAuthorization(_GetMockAuthrizationContext()));
        }