Exemplo n.º 1
0
    def test_check_password(self):
        # All valid
        self.assertTrue(self.__user.check_password(self.__valid_password))

        # Invalid password doesn't exist
        self.assertFalse(self.__user.check_password("self.__valid_password"))

        registered = User()
        registered.register("Eytan", "Eytan's password")

        # Invalid - password exist but isn't corresponding with the username
        self.assertFalse(registered.check_password(self.__valid_password))
Exemplo n.º 2
0
class UserTests(unittest.TestCase):
    def setUp(self):
        self.__valid_name = "anna9218"
        self.__valid_password = "******"
        self.__user = User()
        self.__user.register(self.__valid_name, self.__valid_password)

        self.__product = Product("Eytan's product", 12, "Eytan's category")
        self.__store: Store = Store("myStore")
        self.__products_to_add = [{
            "product": self.__product,
            "store_name": self.__store.get_name(),
            "amount": 1,
            "discount_type": DiscountType.DEFAULT,
            "purchase_type": PurchaseType.DEFAULT
        }]  # products_stores_quantity_ls

    def test_register(self):
        # All valid
        guest1 = User()
        self.assertTrue(guest1.register("valid", "valid"))
        self.assertTrue(guest1.is_registered())
        self.assertTrue(guest1.is_logged_out())
        self.assertFalse(guest1.is_logged_in())

        # All valid -username has white spaces but not empty
        guest11 = User()
        self.assertTrue(
            guest11.register("valid validovich", "valid")['response'])
        self.assertTrue(guest11.is_registered())
        self.assertTrue(guest11.is_logged_out())
        self.assertFalse(guest11.is_logged_in())

        # Invalid - username is empty
        guest2 = User()
        self.assertFalse(guest2.register("", "valid")['response'])
        self.assertFalse(guest2.is_registered())
        self.assertTrue(guest2.is_logged_out())
        self.assertFalse(guest2.is_logged_in())

        # Invalid - password is empty
        self.assertFalse(guest2.register("valid", "")['response'])
        self.assertFalse(guest2.is_registered())
        self.assertTrue(guest2.is_logged_out())
        self.assertFalse(guest2.is_logged_in())

        # Invalid - user already exist
        self.assertFalse(guest1.register("not valid", "not valid")['response'])
        self.assertEqual(guest1.get_nickname(), "valid")

    def test_login(self):
        # All Valid
        self.assertTrue(
            self.__user.login(self.__valid_name,
                              self.__valid_password)['response'])
        self.assertTrue(self.__user.is_logged_in())
        self.assertFalse(self.__user.is_logged_out())

        self.__user.logout()

        # Invalid - username + whitespaces, valid password
        self.assertFalse(
            self.__user.login(self.__valid_name + " ",
                              self.__valid_password)['response'])
        self.assertFalse(self.__user.is_logged_in())
        self.assertTrue(self.__user.is_logged_out())

        # Invalid - valid username, password + whitespaces
        self.assertFalse(
            self.__user.login(self.__valid_name,
                              self.__valid_password + " ")['response'])
        self.assertFalse(self.__user.is_logged_in())
        self.assertTrue(self.__user.is_logged_out())

        # Invalid -username doesn't exist
        self.assertFalse(
            self.__user.login("self.__valid_name",
                              self.__valid_password)['response'])
        self.assertFalse(self.__user.is_logged_in())
        self.assertTrue(self.__user.is_logged_out())

        # Invalid - incorrect password
        self.assertFalse(
            self.__user.login(self.__valid_name,
                              "self.__valid_password")['response'])
        self.assertFalse(self.__user.is_logged_in())
        self.assertTrue(self.__user.is_logged_out())

        # All Valid - second and third try
        self.assertTrue(
            self.__user.login(self.__valid_name,
                              self.__valid_password)['response'])
        self.assertTrue(self.__user.is_logged_in())
        self.assertFalse(self.__user.is_logged_out())
        self.__user.logout()

        self.assertTrue(
            self.__user.login(self.__valid_name,
                              self.__valid_password)['response'])
        self.assertTrue(self.__user.is_logged_in())
        self.assertFalse(self.__user.is_logged_out())
        self.__user.logout()

        registered = User()
        registered.register("Eytan", "Eytan's password")

        # Invalid - password of different registered user
        self.assertFalse(
            self.__user.login(self.__valid_name,
                              "Eytan's password")['response'])
        self.assertFalse(self.__user.is_logged_in())
        self.assertTrue(self.__user.is_logged_out())

        self.__user.login(self.__valid_name, self.__valid_password)

        # Invalid - user already logged in
        self.assertFalse(
            self.__user.login(self.__valid_name,
                              self.__valid_password)['response'])
        self.assertTrue(self.__user.is_logged_in())
        self.assertFalse(self.__user.is_logged_out())

        self.__user.logout()

        # Register and login other users and than try to login again
        self.assertTrue(
            registered.login("Eytan", "Eytan's password")['response'])
        registered.logout()

        other_user = User()
        other_user.register("yarin", "100")
        self.assertTrue(other_user.login("yarin", "100")['response'])
        other_user.logout()
        self.assertTrue(
            self.__user.login(self.__valid_name,
                              self.__valid_password)['response'])

    def test_logout(self):
        self.__user.login(self.__valid_name, self.__valid_password)

        # All valid
        self.assertTrue(self.__user.logout())
        self.assertTrue(self.__user.is_logged_out())
        self.assertFalse(self.__user.is_logged_in())

        # Invalid - user is already logged out
        self.assertFalse(self.__user.logout())
        self.assertTrue(self.__user.is_logged_out())
        self.assertFalse(self.__user.is_logged_in())

        guest = User()

        # Invalid - user isn't register
        self.assertFalse(guest.logout())
        self.assertTrue(guest.is_logged_out())
        self.assertFalse(guest.is_logged_in())

    def test_check_password(self):
        # All valid
        self.assertTrue(self.__user.check_password(self.__valid_password))

        # Invalid password doesn't exist
        self.assertFalse(self.__user.check_password("self.__valid_password"))

        registered = User()
        registered.register("Eytan", "Eytan's password")

        # Invalid - password exist but isn't corresponding with the username
        self.assertFalse(registered.check_password(self.__valid_password))

    def test_check_nickname(self):
        # All valid
        self.assertTrue(self.__user.check_nickname(self.__valid_name))

        # Invalid password doesn't exist
        self.assertFalse(self.__user.check_nickname("self.__valid_password"))

        registered = User()
        registered.register("Eytan", "Eytan's password")

        # Invalid - password exist but isn't corresponding with the username
        self.assertFalse(registered.check_nickname(self.__valid_name))

    def test_is_logged_in(self):
        guest = User()
        subscriber = User()
        subscriber.register("Sub", "scriber")
        self.__user.login(self.__valid_name, self.__valid_password)

        # All valid
        self.assertTrue(self.__user.is_logged_in())

        # Not valid - user is logged out
        self.assertFalse(subscriber.is_logged_in())

        # Not valid - user isn't registered
        self.assertFalse(guest.is_logged_in())

    def test_is_logged_out(self):
        guest = User()
        subscriber = User()
        subscriber.register("Sub", "scriber")
        subscriber.login("Sub", "scriber")
        self.__user.logout()

        # All valid
        self.assertTrue(self.__user.is_logged_out())

        # Not valid - user is logged out
        self.assertFalse(subscriber.is_logged_out())

        # Not valid - user isn't registered
        self.assertTrue(guest.is_logged_out())

    def test_is_registered(self):
        guest = User()
        self.__user.logout()

        # All valid - user is registered
        self.assertTrue(self.__user.is_registered())

        # All valid - user isn't registered
        self.assertFalse(guest.is_registered())

    def test_get_nickname(self):
        guest = User()
        self.__user.logout()

        # All valid - user is registered
        self.assertEqual(self.__user.get_nickname(), self.__valid_name)

        # All valid - user isn't registered
        self.assertIsNone(guest.get_nickname())

    def test_save_products_to_basket(self):
        guest = User()

        # All valid- guest
        self.assertTrue(
            guest.save_products_to_basket(self.__products_to_add)['response'])
        self.assertEqual(
            1,
            len(guest.get_shopping_cart().get_store_basket(
                self.__store.get_name()).get_products()))
        products = [
            product_as_dictionary['product']
            for product_as_dictionary in guest.get_shopping_cart().
            get_store_basket(self.__store.get_name()).get_products()
        ]
        self.assertIn(self.__product, products)

        # Valid - empty products
        self.assertTrue(guest.save_products_to_basket([])['response'])
        self.assertEqual(
            1,
            len(guest.get_shopping_cart().get_store_basket(
                self.__store.get_name()).get_products()))
        products = [
            product_as_dictionary['product']
            for product_as_dictionary in guest.get_shopping_cart().
            get_store_basket(self.__store.get_name()).get_products()
        ]
        self.assertIn(self.__product, products)

        # Invalid - product is None
        self.assertFalse(
            guest.save_products_to_basket([{
                "product":
                None,
                "store_name":
                self.__store.get_name(),
                "amount":
                1,
                "discount_type":
                DiscountType.DEFAULT,
                "purchase_type":
                PurchaseType.DEFAULT
            }])['response'])
        self.assertEqual(
            1,
            len(guest.get_shopping_cart().get_store_basket(
                self.__store.get_name()).get_products()))
        products = [
            product_as_dictionary['product']
            for product_as_dictionary in guest.get_shopping_cart().
            get_store_basket(self.__store.get_name()).get_products()
        ]
        self.assertIn(self.__product, products)

        # Invalid - Edge case - quantity is zero
        self.assertFalse(
            guest.save_products_to_basket([{
                "product":
                self.__product,
                "store_name":
                self.__store.get_name(),
                "amount":
                0,
                "discount_type":
                DiscountType.DEFAULT,
                "purchase_type":
                PurchaseType.DEFAULT
            }])['response'])
        self.assertEqual(
            1,
            len(guest.get_shopping_cart().get_store_basket(
                self.__store.get_name()).get_products()))
        products = [
            product_as_dictionary['product']
            for product_as_dictionary in guest.get_shopping_cart().
            get_store_basket(self.__store.get_name()).get_products()
        ]
        self.assertIn(self.__product, products)

        # Invalid - Negative quantity
        self.assertFalse(
            guest.save_products_to_basket([{
                "product":
                self.__product,
                "store_name":
                self.__store.get_name(),
                "amount":
                -11,
                "discount_type":
                DiscountType.DEFAULT,
                "purchase_type":
                PurchaseType.DEFAULT
            }])['response'])
        self.assertEqual(
            1,
            len(guest.get_shopping_cart().get_store_basket(
                self.__store.get_name()).get_products()))
        products = [
            product_as_dictionary['product']
            for product_as_dictionary in guest.get_shopping_cart().
            get_store_basket(self.__store.get_name()).get_products()
        ]
        self.assertIn(self.__product, products)

    # @logger
    def tearDown(self):
        # maybe delete the registered user resulted from this test
        pass

    def __repr__(self):
        return repr("UserTests")

    if __name__ == '__main__':
        unittest.main()
Exemplo n.º 3
0
class UserTests(unittest.TestCase):
    def setUp(self):
        # self.user.logoutState = StubLogout()
        # self.__valid_name = "anna9218"
        # self.__valid_pass = "******"
        # self.__invalid_input = ""
        # self.__product = StubProduct()
        # self.__store = StubStore()
        # self.__product_ls_to_add = [[self.__product, self.__store, 1]]  # products_stores_quantity_ls

        self.__user = User()
        self.__registration_state_mock = Registration()
        self.__login_state_mock = Login()
        self.__shopping_cart_mock = ShoppingCart()
        self.__purchase_history_mock = []
        self.__purchase_mock = Purchase([], 10.0, "store_name", "username")
        self.__shopping_basket_mock = ShoppingBasket()

        # self.__user.__registration = self.__user.set_registration_state(StubRegistration())
        # # self.__user.__loginState = self.__user.set_login_state(StubLogin())
        # self.__user.__shoppingCart = self.__user.set_shopping_cart(StubShoppingCart())

    def test_register(self):
        # self.assertTrue(self.__user.register(self.__valid_name, self.__valid_pass)['response'])
        # self.assertFalse(self.__user.register(self.__valid_name, self.__valid_pass)['response'])
        self.__user.is_registered = MagicMock(return_value=True)
        res = self.__user.register("nickname", "password")
        self.assertFalse(res['response'])

        self.__user.is_registered = MagicMock(return_value=False)
        self.__user.set_registration_state(self.__registration_state_mock)
        res = self.__user.register("nickname", "password")
        self.assertTrue(res['response'])

    def test_login(self):
        # self.__user.register(self.__valid_name, self.__valid_pass)
        # self.assertTrue(self.__user.login(self.__valid_name, self.__valid_pass)['response'])
        # self.assertFalse(self.__user.login(self.__invalid_input, self.__invalid_input)['response'])
        self.__user.check_password = MagicMock(return_value=False)
        res = self.__user.login("nickname", "password")
        self.assertFalse(res['response'])

        self.__user.check_password = MagicMock(return_value=True)
        self.__user.is_logged_in = MagicMock(return_value=True)
        res = self.__user.login("nickname", "password")
        self.assertFalse(res['response'])

        self.__user.is_logged_in = MagicMock(return_value=False)
        self.__user.check_nickname = MagicMock(return_value=True)
        self.__login_state_mock.login = MagicMock(return_value=True)
        self.__user.set_login_state(self.__login_state_mock)
        self.__user.get_nickname = MagicMock(return_value="TradeManager")
        self.__user.set_registration_state(self.__registration_state_mock)
        res = self.__user.login("nickname", "password")
        self.assertTrue(res['response'])

        self.__user.get_nickname = MagicMock(return_value="nickname")
        self.__user.set_registration_state(self.__registration_state_mock)
        res = self.__user.login("nickname", "password")
        self.assertTrue(res['response'])

    def test_logout(self):
        # self.__user.register(self.__valid_name, self.__valid_pass)
        # self.__user.login(self.__valid_name, self.__valid_pass)
        # self.assertTrue(self.__user.logout())
        self.__user.is_logged_in = MagicMock(return_value=True)
        self.__user.set_login_state(self.__login_state_mock)
        res = self.__user.logout()
        self.assertTrue(res)

        self.__user.is_logged_in = MagicMock(return_value=False)
        self.__user.set_login_state(self.__login_state_mock)
        res = self.__user.logout()
        self.assertFalse(res)

    def test_check_password(self):
        # self.__user.register(self.__valid_name, self.__valid_pass)
        # self.assertTrue(self.__user.check_password(self.__valid_pass))
        # self.assertFalse(self.__user.check_password(self.__invalid_input))

        # hashed value of 'password'
        self.__registration_state_mock.get_password = MagicMock(
            return_value=
            'fcf0a27361d035a079499e86d461e787d8fb328cc3de44df3a96f1d8d00798774cd4375f08353275c2d46a730834e75f3003cf98555b7f19b267b8aaaa5c5b3ae052890976839ca40cc00d16a5c5515093ade62e6d46a550c4cce095c65bb789'
        )
        self.__user.set_registration_state(self.__registration_state_mock)
        self.assertTrue(self.__user.check_password("password"))
        self.assertFalse(self.__user.check_password("wrong password"))

    def test_check_nickname(self):
        # self.__user.register(self.__valid_name, self.__valid_pass)
        # self.assertTrue(self.__user.check_nickname(self.__valid_name))
        # self.assertFalse(self.__user.check_nickname(self.__invalid_input))
        self.__registration_state_mock.get_nickname = MagicMock(
            return_value="nickname")
        self.__user.set_registration_state(self.__registration_state_mock)
        self.assertTrue(self.__user.check_nickname("nickname"))
        self.assertFalse(self.__user.check_nickname("wrong nickname"))

    def test_is_logged_in(self):
        # self.__user.register(self.__valid_name, self.__valid_pass)
        # self.assertFalse(self.__user.is_logged_in())
        # self.__user.login(self.__valid_name, self.__valid_pass)
        # self.assertTrue(self.__user.is_logged_in())
        self.__login_state_mock.is_logged_in = MagicMock(return_value=True)
        self.__user.set_login_state(self.__login_state_mock)
        self.assertTrue(self.__user.is_logged_in())

        self.__login_state_mock.is_logged_in = MagicMock(return_value=False)
        self.__user.set_login_state(self.__login_state_mock)
        self.assertFalse(self.__user.is_logged_in())

    def test_is_logged_out(self):
        # self.__user.register(self.__valid_name, self.__valid_pass)
        # self.assertTrue(self.__user.is_logged_out())
        # self.__user.login(self.__valid_name, self.__valid_pass)
        # self.assertFalse(self.__user.is_logged_out())
        self.__login_state_mock.is_logged_in = MagicMock(return_value=False)
        self.__user.set_login_state(self.__login_state_mock)
        self.assertTrue(self.__user.is_logged_out())

        self.__login_state_mock.is_logged_in = MagicMock(return_value=True)
        self.__user.set_login_state(self.__login_state_mock)
        self.assertFalse(self.__user.is_logged_out())

    def test_is_registered(self):
        # self.assertFalse(self.__user.is_registered())
        # self.__user.register(self.__valid_name, self.__valid_pass)
        # self.assertTrue(self.__user.is_registered())
        self.__registration_state_mock.is_registered = MagicMock(
            return_value=True)
        self.__user.set_registration_state(self.__registration_state_mock)
        self.assertTrue(self.__user.is_registered())

        self.__registration_state_mock.is_registered = MagicMock(
            return_value=False)
        self.__user.set_registration_state(self.__registration_state_mock)
        self.assertFalse(self.__user.is_registered())

    def test_get_nickname(self):
        # self.__user.register(self.__valid_name, self.__valid_pass)
        # self.assertEqual(self.__user.get_nickname(), self.__valid_name, "")
        # self.assertNotEqual(self.__user.get_nickname(), self.__invalid_input, "")
        self.__registration_state_mock.get_nickname = MagicMock(
            return_value="nickname")
        self.__user.set_registration_state(self.__registration_state_mock)
        self.assertEqual(self.__user.get_nickname(), "nickname")
        self.assertNotEqual(self.__user.get_nickname(), "wrong nickname")

    def test_save_products_to_basket(self):
        # # test for guest
        # self.assertTrue(self.__user.save_products_to_basket(self.__product_ls_to_add))
        # # test for subscriber
        # self.__user.register(self.__valid_name, self.__valid_pass)
        # self.assertTrue(self.__user.save_products_to_basket(self.__product_ls_to_add))
        self.__shopping_cart_mock.add_products = MagicMock(
            return_value={
                'response': True,
                'msg': "Products were added to shopping cart successfully"
            })

        self.__user.set_shopping_cart(self.__shopping_cart_mock)
        res = self.__user.save_products_to_basket([])
        self.assertTrue(res['response'])

        self.__shopping_cart_mock.add_products = MagicMock(
            return_value={
                'response': False,
                'msg': "Error! invalid input"
            })

        self.__user.set_shopping_cart(self.__shopping_cart_mock)
        res = self.__user.save_products_to_basket([])
        self.assertFalse(res['response'])

    def test_view_shopping_cart(self):
        self.__shopping_cart_mock.view_shopping_cart = MagicMock(
            return_value={
                'response': [],
                'msg': "Shopping cart was retrieved successfully"
            })

        self.__user.set_shopping_cart(self.__shopping_cart_mock)
        res = self.__user.view_shopping_cart()
        self.assertEqual(res['msg'],
                         "Shopping cart was retrieved successfully")

    def test_remove_from_shopping_cart(self):
        self.__shopping_cart_mock.remove_from_shopping_cart = MagicMock(
            return_value={
                'response': True,
                'msg':
                "Products were removed from your shopping cart successfully"
            })

        self.__user.set_shopping_cart(self.__shopping_cart_mock)
        res = self.__user.remove_from_shopping_cart([])
        self.assertTrue(res['response'])

    def test_update_quantity_in_shopping_cart(self):
        self.__shopping_cart_mock.update_quantity = MagicMock(
            return_value={
                'response': True,
                'msg': "Shopping cart was updated successfully."
            })

        self.__user.set_shopping_cart(self.__shopping_cart_mock)
        res = self.__user.update_quantity_in_shopping_cart([])
        self.assertTrue(res['response'])

    def test_complete_purchase(self):
        self.__shopping_cart_mock.get_store_basket = MagicMock(
            return_value=self.__shopping_basket_mock)
        self.__user.set_shopping_cart(self.__shopping_cart_mock)
        self.__purchase_mock.get_store_name = MagicMock(
            return_value="store_name")
        self.__shopping_basket_mock.complete_purchase = MagicMock(
            return_value=True)
        res = self.__user.complete_purchase(self.__purchase_mock)
        self.assertTrue(res)

    # def test_remove_purchase(self):
    #     res = self.__user.remove_purchase(self.__purchase_mock)
    #     self.assertTrue(res)

    def test_get_password(self):
        self.__registration_state_mock.get_password = MagicMock(
            return_value="password")
        self.__user.set_registration_state(self.__registration_state_mock)
        self.assertEqual(self.__user.get_password(), "password")
        self.assertNotEqual(self.__user.get_password(), "wrong password")

    def tearDown(self):
        # maybe delete the registered user resulted from this test
        pass

    def __repr__(self):
        return repr("UserTests")

    if __name__ == '__main__':
        unittest.main()