Ejemplo n.º 1
0
class TestWinner(unittest.TestCase):
    def setUp(self):
        super(TestWinner, self).setUp()
        self.store = FakeStore()
        self.user = self.store.create(User,
                                      name="Foo Bar",
                                      email="*****@*****.**",
                                      password="******")

        self.categories = CATEGORIES.contest(config.CONTEST_ID)
        self.winner = self.store.create(Winner, contest=config.CONTEST_ID)
        self.winner.add_winner(self.categories[0].key,
                               self.categories[0].nominees[0].key)
        self.winner.add_winner(self.categories[1].key,
                               self.categories[1].nominees[1].key)

    def test_winner(self):
        self.assertEqual(2, len(self.winner.winners))
        self.assertEqual(self.winner.progress, 2.0 / len(self.categories))

    def test_winner_points(self):
        votes = Votes(email=self.user.email, contest=config.CONTEST_ID)
        votes.add_vote(self.categories[0].key,
                       self.categories[0].nominees[0].key)
        votes.add_vote(self.categories[1].key,
                       self.categories[1].nominees[0].key)

        points = self.winner.get_points(votes)
        self.assertEqual(self.categories[0].points, points)

        missed_points = self.winner.get_missed_points(votes)
        self.assertEqual(self.categories[1].points, missed_points)

        self.assertEqual(sum([c.points for c in self.categories][2:]),
                         self.winner.remaining_points)
Ejemplo n.º 2
0
class TestWinner(unittest.TestCase):

    def setUp(self):
        super(TestWinner, self).setUp()
        self.store = FakeStore()
        self.user = self.store.create(
            User, name="Foo Bar", email="*****@*****.**", password="******")

        self.categories = CATEGORIES.contest(config.CONTEST_ID)
        self.winner = self.store.create(Winner, contest=config.CONTEST_ID)
        self.winner.add_winner(
            self.categories[0].key, self.categories[0].nominees[0].key)
        self.winner.add_winner(
            self.categories[1].key, self.categories[1].nominees[1].key)

    def test_winner(self):
        self.assertEqual(2, len(self.winner.winners))
        self.assertEqual(self.winner.progress, 2.0 / len(self.categories))

    def test_winner_points(self):
        votes = Votes(email=self.user.email, contest=config.CONTEST_ID)
        votes.add_vote(
            self.categories[0].key, self.categories[0].nominees[0].key)
        votes.add_vote(
            self.categories[1].key, self.categories[1].nominees[0].key)

        points = self.winner.get_points(votes)
        self.assertEqual(self.categories[0].points, points)

        missed_points = self.winner.get_missed_points(votes)
        self.assertEqual(self.categories[1].points, missed_points)

        self.assertEqual(
            sum([c.points for c in self.categories][2:]),
            self.winner.remaining_points)
Ejemplo n.º 3
0
    def setUp(self):
        super(TestUser, self).setUp()
        self.store = FakeStore()

        with mock.patch("uuid.uuid4") as mock_uuid:
            mock_uuid.return_value.hex = "tokenvalue"
            self.user = User.use(self.store)(
                name="Foo Bar", email="*****@*****.**", password="******")
Ejemplo n.º 4
0
class TestVotes(unittest.TestCase):

    def setUp(self):
        super(TestVotes, self).setUp()
        self.store = FakeStore()
        self.user = self.store.create(
            User, name="Foo Bar", email="*****@*****.**", password="******")

        self.votes = self.store.create(
            Votes, email=self.user.email, contest=config.CONTEST_ID)
        self.categories = CATEGORIES.contest(config.CONTEST_ID)
        category = self.categories[0]
        self.votes.add_vote(category.key, category.nominees[0].key)

        category = self.categories[1]
        self.votes.add_vote(category.key, category.nominees[1].key)

    def test_votes(self):
        self.store.save(self.votes)
        self.assertEqual(self.votes.progress, 2.0 / len(self.categories))

    def test_get_votes(self):
        self.store.save(self.votes)
        vote = self.votes.get_vote(self.categories[0].key)
        self.assertEqual(vote, self.categories[0].nominees[0].key)
        self.assertEqual(None, self.votes.get_vote("hardly likeley"))

    def test_fetch_votes(self):
        self.store.save(self.votes)
        result = self.store.fetch(
            Votes, "{0}:[email protected]".format(config.CONTEST_ID))
        self.assertEqual(2, len(result.votes))
Ejemplo n.º 5
0
    def setUp(self):
        super(TestVotes, self).setUp()
        self.store = FakeStore()
        self.user = self.store.create(
            User, name="Foo Bar", email="*****@*****.**", password="******")

        self.votes = self.store.create(
            Votes, email=self.user.email, contest=config.CONTEST_ID)
        self.categories = CATEGORIES.contest(config.CONTEST_ID)
        category = self.categories[0]
        self.votes.add_vote(category.key, category.nominees[0].key)

        category = self.categories[1]
        self.votes.add_vote(category.key, category.nominees[1].key)
Ejemplo n.º 6
0
    def setUp(self):
        super(TestWinner, self).setUp()
        self.store = FakeStore()
        self.user = self.store.create(User,
                                      name="Foo Bar",
                                      email="*****@*****.**",
                                      password="******")

        self.categories = CATEGORIES.contest(config.CONTEST_ID)
        self.winner = self.store.create(Winner, contest=config.CONTEST_ID)
        self.winner.add_winner(self.categories[0].key,
                               self.categories[0].nominees[0].key)
        self.winner.add_winner(self.categories[1].key,
                               self.categories[1].nominees[1].key)
Ejemplo n.º 7
0
    def setUp(self):
        super(TestUser, self).setUp()
        self.store = FakeStore()

        with mock.patch("uuid.uuid4") as mock_uuid:
            mock_uuid.return_value.hex = "tokenvalue"
            self.user = User.use(self.store)(
                name="Foo Bar", email="*****@*****.**", password="******")
Ejemplo n.º 8
0
    def setUp(self):
        super(TestWinner, self).setUp()
        self.store = FakeStore()
        self.user = self.store.create(
            User, name="Foo Bar", email="*****@*****.**", password="******")

        self.categories = CATEGORIES.contest(config.CONTEST_ID)
        self.winner = self.store.create(Winner, contest=config.CONTEST_ID)
        self.winner.add_winner(
            self.categories[0].key, self.categories[0].nominees[0].key)
        self.winner.add_winner(
            self.categories[1].key, self.categories[1].nominees[1].key)
Ejemplo n.º 9
0
class TestUser(unittest.TestCase):

    def setUp(self):
        super(TestUser, self).setUp()
        self.store = FakeStore()

        with mock.patch("uuid.uuid4") as mock_uuid:
            mock_uuid.return_value.hex = "tokenvalue"
            self.user = User.use(self.store)(
                name="Foo Bar", email="*****@*****.**", password="******")

    def test_user_authenticate(self):
        self.assertTrue(self.user.authenticate("foobar"))

    def test_user_admin_field(self):
        self.assertFalse(self.user.admin)
        self.user.admin = True
        self.user.store.save()
        user = self.store.fetch(User, self.user.email)
        self.assertTrue(user.admin)

    def test_user_hash_password(self):
        data = self.user.to_dict()
        self.assertFalse(data["password"] == "foobar")

    def test_user_save(self):
        self.user.store.save()
        user = User.use(self.store).store.fetch(self.user.id)
        self.assertEqual(user, self.user)
        self.assertEqual("tokenvalue", user.token)

    def test_token(self):
        token = self.store.create(
            Token, email=self.user.email, token=self.user.token)
        self.assertEqual("tokenvalue", token.id)
        self.assertEqual(self.user.email, token.email)
        self.assertEqual(self.user.token, token.id)

    @mock.patch("time.time")
    def test_generate_reset_password_url(self, mock_time):
        mock_time.return_value = 1000
        expected_signature = hashlib.sha256((
                "http://localhost/" + "*****@*****.**" + self.user.password +
                self.user.token + "4600" + config.PASSWORD_SALT).encode("utf8")
            ).hexdigest()
        expected_url = "http://localhost/?{0}".format(urllib.parse.urlencode({
            "reset_signature": expected_signature,
            "reset_expiration": 4600,
            "reset_email": self.user.email
        }))

        actual_url = self.user.generate_reset_password_url("http://localhost/")
        self.assertEqual(expected_url, actual_url)

    def test_verify_reset_password_url(self):
        signed_url = self.user.generate_reset_password_url("http://foo.com")
        self.assertTrue(self.user.verify_reset_password_url(signed_url))

    @mock.patch("time.time")
    def test_verify_reset_password_url_expired(self, mock_time):
        mock_time.return_value = 1000
        signed_url = self.user.generate_reset_password_url("http://foo.com")
        mock_time.return_value = 5000
        self.assertFalse(self.user.verify_reset_password_url(signed_url))
Ejemplo n.º 10
0
class TestUser(unittest.TestCase):

    def setUp(self):
        super(TestUser, self).setUp()
        self.store = FakeStore()

        with mock.patch("uuid.uuid4") as mock_uuid:
            mock_uuid.return_value.hex = "tokenvalue"
            self.user = User.use(self.store)(
                name="Foo Bar", email="*****@*****.**", password="******")

    def test_user_authenticate(self):
        self.assertTrue(self.user.authenticate("foobar"))

    def test_user_admin_field(self):
        self.assertFalse(self.user.admin)
        self.user.admin = True
        self.user.store.save()
        user = self.store.fetch(User, self.user.email)
        self.assertTrue(user.admin)

    def test_user_hash_password(self):
        data = self.user.to_dict()
        self.assertFalse(data["password"] == "foobar")

    def test_user_save(self):
        self.user.store.save()
        user = User.use(self.store).store.fetch(self.user.id)
        self.assertEqual(user, self.user)
        self.assertEqual("tokenvalue", user.token)

    def test_token(self):
        token = self.store.create(
            Token, email=self.user.email, token=self.user.token)
        self.assertEqual("tokenvalue", token.id)
        self.assertEqual(self.user.email, token.email)
        self.assertEqual(self.user.token, token.id)

    @mock.patch("time.time")
    def test_generate_reset_password_url(self, mock_time):
        mock_time.return_value = 1000
        expected_signature = hashlib.sha256(
            "http://localhost/" + "*****@*****.**" + self.user.password +
            self.user.token + "4600" + config.PASSWORD_SALT).hexdigest()
        expected_url = "http://localhost/?{0}".format(urllib.urlencode({
            "reset_signature": expected_signature,
            "reset_email": self.user.email,
            "reset_expiration": 4600
        }))

        actual_url = self.user.generate_reset_password_url("http://localhost/")
        self.assertEqual(expected_url, actual_url)

    def test_verify_reset_password_url(self):
        signed_url = self.user.generate_reset_password_url("http://foo.com")
        self.assertTrue(self.user.verify_reset_password_url(signed_url))

    @mock.patch("time.time")
    def test_verify_reset_password_url_expired(self, mock_time):
        mock_time.return_value = 1000
        signed_url = self.user.generate_reset_password_url("http://foo.com")
        mock_time.return_value = 5000
        self.assertFalse(self.user.verify_reset_password_url(signed_url))