Exemplo n.º 1
0
 def test_equals_returns_false_for_unequal_objects(self):
     now = datetime.datetime.now()
     c1 = User(id=1, username="******", provider=AuthProvider.local,
               email="bob@bob", joined_at_utc=now, active=True,
               admin=False)
     c2 = User(id=2, username="******", provider=AuthProvider.local,
               email="tony@tony", joined_at_utc=now, active=True,
               admin=False)
     self.assertFalse(c1 == c2)
Exemplo n.º 2
0
 def test_hash_is_consistent_for_equal_objects(self):
     now = datetime.datetime.now()
     c1 = User(id=1, username="******", provider=AuthProvider.local,
               email="bob@bob", joined_at_utc=now, active=True,
               admin=False)
     c2 = User(id=1, username="******", provider=AuthProvider.local,
               email="bob@bob", joined_at_utc=now, active=True,
               admin=False)
     self.assertTrue(hash(c1) == hash(c2))
Exemplo n.º 3
0
 def test_get_all_with_more_than_one_user_returns_all_users(self):
     repo = UserRepo(TEST_SESSION_FACTORY)
     u1 = User(username="******", email="good@somewhere")
     u2 = User(username="******", email="agood@somewhere")
     u3 = User(username="******", email="bgood@somewhere")
     repo.add_user(u1)
     repo.add_user(u2)
     repo.add_user(u3)
     users = repo.get_all_users()
     expected = [u1, u2, u3]
     self.assertEqual(len(expected), len(users))
     self.assertListEqual(expected, users)
Exemplo n.º 4
0
 def test_get_all_with_one_user_returns_that_user(self):
     repo = UserRepo(TEST_SESSION_FACTORY)
     u = User(username="******", email="good@somewhere")
     repo.add_user(u)
     users = repo.get_all_users()
     self.assertEqual(1, len(users))
     self.assertEqual(u, users[0])
Exemplo n.º 5
0
def add_default_user():
    """Adds the default user to the database.

    Returns the User added or None if unable to add the user.

    Exceptions:
    LookupError -- if the user already exists
    sqlalchemy.exc.SQLAlchemyError -- if an error occurs accessing the database
    """
    user = None
    session = SESSION_FACTORY()
    try:
        user = session.query(User).\
            filter(User.username == "default").one_or_none()
        if not user:
            user = User(username="******", email="*****@*****.**")
            session.add(user)
            session.commit()
            session.refresh(user)  # refresh so contains data added on insert
            session.expunge(user)  # detatch user from the session
        else:
            session.expunge(user)
            raise LookupError(user)
    except SQLAlchemyError:
        session.rollback()
        raise
    finally:
        session.close()

    return user
    def test_can_add_two_catalogs_with_same_name_and_different_owner(self):
        repo = CatalogRepo(TEST_SESSION_FACTORY)
        user_repo = UserRepo(TEST_SESSION_FACTORY)

        bob = User(username="******", email="bob@bob")
        user_repo.add_user(bob)
        tony = User(username="******", email="tony@tony")
        user_repo.add_user(tony)

        expected_catalogs = []
        catalog = Catalog(owner_id=bob.id, name="A")
        expected_catalogs.append(catalog)
        repo.add(catalog)
        catalog = Catalog(owner_id=tony.id, name="A")
        repo.add(catalog)
        expected_catalogs.append(catalog)

        self.assertEqual(sorted(expected_catalogs), sorted(repo.get_all()))
    def test_cannot_add_unamed_catalog(self):
        repo = CatalogRepo(TEST_SESSION_FACTORY)
        user_repo = UserRepo(TEST_SESSION_FACTORY)

        owner = User(username="******", email="bob@bob")
        user_repo.add_user(owner)
        catalog = Catalog(owner_id=owner.id)
        with self.assertRaises(SQLAlchemyError):
            repo.add(catalog)
Exemplo n.º 8
0
    def setUp(self):
        """Reset the database before each test is run"""
        CategoryRepoTest._HELPER.reset_database()

        # add a test owners
        user_repo = UserRepo(TEST_SESSION_FACTORY)
        self._test_owner = User(username="******", email="bob@bob")
        self._test_owner2 = User(username="******", email="tony@tony")
        user_repo.add_user(self._test_owner)
        user_repo.add_user(self._test_owner2)

        # add a test catalogs
        catalogRepo = CatalogRepo(TEST_SESSION_FACTORY)
        self._test_catalog = Catalog(owner_id=self._test_owner.id,
                                     name="catalog")
        self._test_catalog2 = Catalog(owner_id=self._test_owner2.id,
                                      name="catalog two")
        catalogRepo.add(self._test_catalog)
        catalogRepo.add(self._test_catalog2)
Exemplo n.º 9
0
    def test_update_user_with_known_user_performs_update(self):
        repo = UserRepo(TEST_SESSION_FACTORY)
        u = User(username="******",
                 email="good@somewhere",
                 admin=False,
                 provider=AuthProvider.google,
                 active=False)
        repo.add_user(u)

        u_pre_change_actual = repo.get_by_username("goodusername",
                                                   AuthProvider.google)
        self.assertEqual(u, u_pre_change_actual)

        u_activated = User(username="******",
                           email="good@somewhere",
                           provider=AuthProvider.google,
                           admin=False,
                           active=True)
        repo.update_user(u_activated)

        u_actual = repo.get_by_username("goodusername", AuthProvider.google)
        self.assertEqual(u_activated, u_actual)
    def test_can_get_catalog_by_id_when_catalog_with_owner_and_name_added(self):
        repo = CatalogRepo(TEST_SESSION_FACTORY)
        user_repo = UserRepo(TEST_SESSION_FACTORY)

        owner = User(username="******", email="bob@bob")
        user_repo.add_user(owner)
        catalog = Catalog(owner_id=owner.id, name="Bob's catalog.")
        repo.add(catalog)

        actual_catalog = repo.get_by_id(catalog.id)
        self.assertEqual(owner.id, actual_catalog.owner_id)
        self.assertEqual(catalog.name, actual_catalog.name)
        self.assertIsNone(actual_catalog.description)
        self.assertIsNotNone(actual_catalog.created_at_utc)
    def test_get_all_returns_all_when_catalog_contains_one_entry(self):
        repo = CatalogRepo(TEST_SESSION_FACTORY)
        user_repo = UserRepo(TEST_SESSION_FACTORY)

        owner = User(username="******", email="bob@bob")
        user_repo.add_user(owner)
        catalog = Catalog(owner_id=owner.id, name="Bob's catalog.")
        repo.add(catalog)

        catalogs = repo.get_all()
        self.assertEqual(1, len(catalogs))
        self.assertEqual(owner.id, catalogs[0].owner_id)
        self.assertEqual(catalog.name, catalogs[0].name)
        self.assertIsNone(catalogs[0].description)
        self.assertIsNotNone(catalogs[0].created_at_utc)
    def test_get_all_returns_all_when_catalog_contains_more_than_one_entry(self):
        repo = CatalogRepo(TEST_SESSION_FACTORY)
        user_repo = UserRepo(TEST_SESSION_FACTORY)

        owner = User(username="******", email="bob@bob")
        user_repo.add_user(owner)
        expected_catalogs = []
        expected_catalogs.append(Catalog(owner_id=owner.id, name="A"))
        expected_catalogs.append(Catalog(owner_id=owner.id, name="B"))
        expected_catalogs.append(Catalog(owner_id=owner.id, name="C"))

        for catalog in expected_catalogs:
            repo.add(catalog)

        actual_catalogs = repo.get_all()
        self.assertEqual(sorted(expected_catalogs), sorted(actual_catalogs))
Exemplo n.º 13
0
def google_register():
    """Register a user using a google provider."""
    # protect against CSFR
    if request.args.get('state') != session['state']:
        response = make_response(json.dumps('Invalid state parameter.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response

    one_time_code = request.values["code"]
    credentials = None
    try:
        # upgrade one-time code from client into oauth2 credentials
        secrets =\
            json.loads(resource_string(__name__,
                                       "client_secrets.json").decode("utf-8"))
        credentials =\
            client.credentials_from_code(WEBAPP_CLIENT_ID,
                                         secrets["web"]["client_secret"],
                                         "",
                                         one_time_code)
    except client.FlowExchangeError:
        response =\
            make_response(json.dumps('Failed to upgrade google auth code.'),
                          401)
        response.headers['Content-Type'] = 'application/json'
        return response

    username = credentials.id_token["sub"]
    if not _USER_REPO.exists_by_username(username, AuthProvider.google):
        # add user
        user = User(username=username,
                    provider=AuthProvider.google,
                    email=credentials.id_token["email"])
        _USER_REPO.add_user(user)
        print("Added user")
        return "", 200

    print("User already registered.")
    response =\
        make_response(json.dumps('User already registered.'), 409)
    response.headers['Content-Type'] = 'application/json'
    return response
Exemplo n.º 14
0
 def test_hash_is_consistent_when_called_many_time(self):
     now = datetime.datetime.now()
     c = User(id=1, username="******", provider=AuthProvider.local,
              email="bob@bob", joined_at_utc=now, active=True,
              admin=False)
     self.assertEqual(hash(c), hash(c))
Exemplo n.º 15
0
 def test_equals_returns_true_for_same_object(self):
     now = datetime.datetime.now()
     c = User(id=1, username="******", provider=AuthProvider.local,
              email="bob@bob", joined_at_utc=now, active=True,
              admin=False)
     self.assertTrue(c == c)
Exemplo n.º 16
0
 def test_get_user_by_email_returns_correct_user_when_user_exists(self):  # pylint: disable=line-too-long
     repo = UserRepo(TEST_SESSION_FACTORY)
     u = User(username="******", email="good@somewhere")
     repo.add_user(u)
     actual_u = repo.get_by_email("good@somewhere")
     self.assertEqual(u, actual_u)
Exemplo n.º 17
0
 def test_get_user_by_email_returns_none_when_no_matching_user(self):
     repo = UserRepo(TEST_SESSION_FACTORY)
     u = User(username="******", email="good@somewhere")
     repo.add_user(u)
     self.assertIsNone(repo.get_by_email("bad@nowhere"))
Exemplo n.º 18
0
 def test_exists_by_username_returns_true_when_user_exists(self):
     repo = UserRepo(TEST_SESSION_FACTORY)
     repo.add_user(User(username="******", email="good@somewhere"))
     self.assertTrue(repo.exists_by_username("goodusername"))
Exemplo n.º 19
0
 def test_update_user_with_unknown_user_raises_lookuperror(self):
     repo = UserRepo(TEST_SESSION_FACTORY)
     u = User(username="******", email="good@somewhere")
     with self.assertRaises(LookupError):
         repo.update_user(u)