Exemplo n.º 1
0
 def test_delete(self) -> None:
     """Add a new session and remove it directly."""
     assert User.count() == 4 and Client.count() == 4 and Session.count(
     ) == 4
     user = User.add({
         'first_name': 'James',
         'last_name': 'Bond',
         'email': '*****@*****.**',
         'alias': '007',
         'data': {
             'user_type': 'amateur',
             'drink_of_choice': 'martini'
         }
     })
     assert User.count() == 5 and Client.count() == 4 and Session.count(
     ) == 4
     key, secret, client = Client.new(user.id)
     assert User.count() == 5 and Client.count() == 5 and Session.count(
     ) == 4
     Session.new(user.id)
     assert User.count() == 5 and Client.count() == 5 and Session.count(
     ) == 5
     Session.delete(Session.from_client(client.id).id)
     assert User.count() == 5 and Client.count() == 5 and Session.count(
     ) == 4
     User.delete(user.id)  # NOTE: deletes client
     assert User.count() == 4 and Client.count() == 4 and Session.count(
     ) == 4
Exemplo n.º 2
0
 def test_delete_facility_map_cascade(self) -> None:
     """Create a new facility, associate it with a user, then remove."""
     assert User.count() == 4 and Facility.count() == 4 and FacilityMap.count() == 4
     Facility.add({'name': 'Bourne_4m', 'latitude': -24.5, 'longitude': -69.25, 'elevation': 5050,
                   'limiting_magnitude': 17.5, 'data': {'telescope_design': 'reflector'}})
     facility = Facility.from_name('Bourne_4m')
     user = User.from_alias('delta_one')
     user.add_facility(facility.id)
     assert User.count() == 4 and Facility.count() == 5 and FacilityMap.count() == 5
     Facility.delete(facility.id)
     assert User.count() == 4 and Facility.count() == 4 and FacilityMap.count() == 4
Exemplo n.º 3
0
 def test_email_already_exists(self) -> None:
     """Test exception on user `email` already exists."""
     with pytest.raises(IntegrityError):
         User.add({
             'first_name': 'Bruce',
             'last_name': 'Wayne',
             'email': '*****@*****.**',
             'alias': 'batman',
             'data': {
                 'user_type': 'amateur'
             }
         })
Exemplo n.º 4
0
 def test_alias_already_exists(self) -> None:
     """Test exception on user `alias` already exists."""
     with pytest.raises(IntegrityError):
         User.add({
             'first_name': 'Bryce',
             'last_name': 'Wayne',
             'email': '*****@*****.**',
             'alias': 'tomb_raider',
             'data': {
                 'user_type': 'amateur'
             }
         })
Exemplo n.º 5
0
 def test_delete_facility_map_cascade(self) -> None:
     """Create a new user, with facility, then remove."""
     assert User.count() == 4 and Facility.count(
     ) == 4 and FacilityMap.count() == 4
     User.add({
         'first_name': 'James',
         'last_name': 'Bond',
         'email': '*****@*****.**',
         'alias': '007',
         'data': {
             'user_type': 'amateur',
             'drink_of_choice': 'martini'
         }
     })
     user = User.from_alias('007')
     Facility.add({
         'name': 'Bond_4m',
         'latitude': -25.5,
         'longitude': -69.25,
         'elevation': 5050,
         'limiting_magnitude': 17.5,
         'data': {
             'telescope_design': 'reflector'
         }
     })
     facility = Facility.from_name('Bond_4m')
     user.add_facility(facility.id)
     assert user.facilities()[0].to_dict() == facility.to_dict()
     assert User.count() == 5 and Facility.count(
     ) == 5 and FacilityMap.count() == 5
     User.delete(user.id)
     assert User.count() == 4 and Facility.count(
     ) == 5 and FacilityMap.count() == 4
     Facility.delete(facility.id)
     assert Facility.count() == 4
Exemplo n.º 6
0
 def test_delete_client_cascade(self) -> None:
     """Add a new user, client, and session. Remove user to clear client and session."""
     assert User.count() == 4 and Client.count() == 4 and Session.count(
     ) == 4
     user = User.add({
         'first_name': 'James',
         'last_name': 'Bond',
         'email': '*****@*****.**',
         'alias': '007',
         'data': {
             'user_type': 'amateur',
             'drink_of_choice': 'martini'
         }
     })
     assert User.count() == 5 and Client.count() == 4 and Session.count(
     ) == 4
     Client.new(user.id)
     assert User.count() == 5 and Client.count() == 5 and Session.count(
     ) == 4
     Session.new(user.id)
     assert User.count() == 5 and Client.count() == 5 and Session.count(
     ) == 5
     User.delete(user.id)
     assert User.count() == 4 and Client.count() == 4 and Session.count(
     ) == 4
Exemplo n.º 7
0
 def test_add_user(self) -> None:
     """Test adding a user and then removing it."""
     facility = Facility.from_name('Croft_4m')
     users = facility.users()
     assert len(users) == 1 and users[0].alias == 'tomb_raider'
     User.add({'first_name': 'James', 'last_name': 'Bond', 'email': '*****@*****.**',
               'alias': '007', 'data': {'user_type': 'amateur', 'drink_of_choice': 'martini'}})
     new_user = User.from_alias('007')
     facility.add_user(new_user.id)
     users = facility.users()
     assert len(users) == 2 and set(u.alias for u in users) == {'tomb_raider', '007'}
     User.delete(new_user.id)
     users = facility.users()
     assert len(users) == 1 and users[0].alias == 'tomb_raider'
Exemplo n.º 8
0
 def test_update_email(self) -> None:
     """Update email address of user profile."""
     old_email, new_email = '*****@*****.**', '*****@*****.**'
     user = User.from_alias('delta_one')
     assert user.email == old_email
     User.update(user.id, email=new_email)
     assert User.from_alias('delta_one').email == User.from_email(
         new_email).email
     User.update(user.id, email=old_email)
     assert User.from_alias('delta_one').email == User.from_email(
         old_email).email
Exemplo n.º 9
0
 def test_for_user_with_group_id_2(self) -> None:
     """Test query for all recommendations for a given user and group."""
     user_id = User.from_alias('tomb_raider').id
     results = Recommendation.for_user(user_id, group_id=2)
     assert len(results) == 4
     for record in results:
         assert record.user_id == user_id
         assert record.group_id == 2
Exemplo n.º 10
0
 def test_update_data(self) -> None:
     """Update custom data of user profile."""
     old_data = {'user_type': 'amateur'}
     new_data = {'user_type': 'amateur', 'special_field': 42}
     user_id = User.from_alias('tomb_raider').id
     assert User.from_id(user_id).data == old_data
     User.update(user_id, special_field=42)
     assert User.from_id(user_id).data == new_data
     User.update(user_id, data=old_data)
     assert User.from_id(user_id).data == old_data
Exemplo n.º 11
0
 def test_new_secret(self) -> None:
     """Generate a new client secret and then manually reset it back."""
     user = User.from_alias('tomb_raider')
     old_hash = Client.from_user(user.id).secret
     key, secret = Client.new_secret(user.id)
     new_hash = secret.hashed().value
     assert new_hash != old_hash
     Client.update(Client.from_user(user.id).id, secret=old_hash)
     assert Client.from_user(user.id).secret == old_hash
Exemplo n.º 12
0
 def test_new_key_and_secret(self) -> None:
     """Generate a new client key and secret and then manually reset them."""
     user = User.from_alias('tomb_raider')
     data = Client.from_user(user.id).to_dict()
     old_key, old_secret_hash = data['key'], data['secret']
     key, secret = Client.new_key(user.id)
     assert key.value != old_key and secret.hashed().value != old_secret_hash
     Client.update(Client.from_key(key.value).id, key=old_key, secret=old_secret_hash)
     client = Client.from_user(user.id)
     assert client.key == old_key and client.secret == old_secret_hash
Exemplo n.º 13
0
 def test_delete(self) -> None:
     """Add a new user and client. Remove the client directly."""
     assert User.count() == 4 and Client.count() == 4
     user = User.add({'first_name': 'James', 'last_name': 'Bond', 'email': '*****@*****.**',
                      'alias': '007', 'data': {'user_type': 'amateur', 'drink_of_choice': 'martini'}})
     assert User.count() == 5 and Client.count() == 4
     key, secret, client = Client.new(user.id)
     assert User.count() == 5 and Client.count() == 5
     Client.delete(client.id)
     assert User.count() == 5 and Client.count() == 4
     User.delete(user.id)
     assert User.count() == 4 and Client.count() == 4
Exemplo n.º 14
0
 def test_delete(self) -> None:
     """Add a new user record and then remove it."""
     assert User.count() == 4
     User.add({
         'first_name': 'James',
         'last_name': 'Bond',
         'email': '*****@*****.**',
         'alias': '007',
         'data': {
             'user_type': 'amateur',
             'drink_of_choice': 'martini'
         }
     })
     assert User.count() == 5
     assert User.from_alias('007').last_name == 'Bond'
     User.delete(User.from_alias('007').id)
     assert User.count() == 4
Exemplo n.º 15
0
 def test_delete_user_cascade(self) -> None:
     """Add a new user and client record and then remove them."""
     assert User.count() == 4 and Client.count() == 4
     user = User.add({'first_name': 'James', 'last_name': 'Bond', 'email': '*****@*****.**',
                      'alias': '007', 'data': {'user_type': 'amateur', 'drink_of_choice': 'martini'}})
     assert User.count() == 5 and Client.count() == 4
     Client.new(user.id)
     assert User.count() == 5 and Client.count() == 5
     User.delete(user.id)
     assert User.count() == 4 and Client.count() == 4
Exemplo n.º 16
0
 def test_embedded(self) -> None:
     """Test embedded method to check JSON-serialization and full join."""
     assert Recommendation.from_id(1).to_json(join=True) == {
         'id':
         1,
         'group_id':
         1,
         'tag_id':
         1,
         'time':
         '2020-10-24 20:02:00' +
         ('' if config.backend == 'sqlite' else '-04:00'),
         'priority':
         1,
         'object_id':
         1,
         'facility_id':
         1,
         'user_id':
         2,
         'forecast_id':
         1,
         'predicted_observation_id':
         11,
         'observation_id':
         19,
         'accepted':
         True,
         'rejected':
         False,
         'data': {},
         'group':
         RecommendationGroup.from_id(1).to_json(join=True),
         'tag':
         RecommendationTag.from_id(1).to_json(join=True),
         'user':
         User.from_id(2).to_json(join=True),
         'facility':
         Facility.from_id(1).to_json(join=True),
         'object':
         Object.from_id(1).to_json(join=True),
         'forecast':
         Forecast.from_id(1).to_json(join=True),
         'predicted':
         Observation.from_id(11).to_json(join=True),
         'observed':
         Observation.from_id(19).to_json(join=True),
     }
Exemplo n.º 17
0
    def test_next(self) -> None:
        """Test query for latest recommendation."""

        user_id = User.from_alias('tomb_raider').id
        response = Recommendation.next(user_id=user_id)
        assert len(response) == 0  # NOTE: all accepted already

        rec_id = Recommendation.for_user(user_id)[0].id
        Recommendation.update(rec_id, accepted=False)

        response = Recommendation.next(user_id=user_id)
        assert len(response) == 1

        Recommendation.update(rec_id, accepted=True)
        response = Recommendation.next(user_id=user_id)
        assert len(response) == 0
Exemplo n.º 18
0
 def test_embedded(self) -> None:
     """Test embedded method to check JSON-serialization and auto-join."""
     assert Client.from_user(User.from_alias('delta_one').id).to_json(join=True) == {
         'id': 2,
         'user_id': 2,
         'level': 10,
         'key': '78h6IuhW30Re7I-C',
         'secret': '7ccb08b171f4a28e6b5f2af5597153873d7cd90a972f2bee7b8ac82c43e0e4e9',
         'valid': True,
         'created': '2020-10-23 17:45:01' + ('' if config.backend == 'sqlite' else '-04:00'),
         'user': {
             'id': 2,
             'first_name': 'Jason',
             'last_name': 'Bourne',
             'email': '*****@*****.**',
             'alias': 'delta_one',
             'data': {
                 'user_type': 'amateur'
             }
         }
     }
Exemplo n.º 19
0
 def test_add_facility(self) -> None:
     """Test adding a facility and then removing it."""
     user = User.from_alias('tomb_raider')
     facilities = user.facilities()
     assert len(facilities) == 2 and set(
         f.name for f in facilities) == {'Croft_1m', 'Croft_4m'}
     Facility.add({
         'name': 'Croft_10m',
         'latitude': -25.5,
         'longitude': -69.25,
         'elevation': 5050,
         'limiting_magnitude': 20.5
     })
     new_facility = Facility.from_name('Croft_10m')
     user.add_facility(new_facility.id)
     facilities = user.facilities()
     assert len(facilities) == 3 and set(
         f.name
         for f in facilities) == {'Croft_1m', 'Croft_4m', 'Croft_10m'}
     user.delete_facility(new_facility.id)
     Facility.delete(new_facility.id)
     facilities = user.facilities()
     assert len(facilities) == 2 and set(
         f.name for f in facilities) == {'Croft_1m', 'Croft_4m'}
Exemplo n.º 20
0
 def test_tuple(self, testdata: TestData) -> None:
     """Test tuple-conversion."""
     for data in testdata['user']:
         user = User.from_dict(data)
         assert tuple(data.values()) == user.to_tuple()
Exemplo n.º 21
0
 def test_facilities(self) -> None:
     """Access associated user facilities."""
     facilities = User.from_alias('tomb_raider').facilities()
     assert all(isinstance(facility, Facility) for facility in facilities)
     assert len(facilities) == 2
Exemplo n.º 22
0
 def test_delete_missing(self) -> None:
     """Test exception on attempt to delete non-existent user."""
     with pytest.raises(NotFound):
         User.delete(-1)
Exemplo n.º 23
0
 def test_from_alias(self, testdata: TestData) -> None:
     """Test loading user profile from `alias`."""
     for user in testdata['user']:
         assert User.from_alias(user['alias']).alias == user['alias']
Exemplo n.º 24
0
 def test_from_email(self, testdata: TestData) -> None:
     """Test loading user profile from `email`."""
     for user in testdata['user']:
         assert User.from_email(user['email']).email == user['email']
Exemplo n.º 25
0
 def test_from_email_missing(self) -> None:
     """Test exception on missing user `email`."""
     with pytest.raises(NotFound):
         User.from_email('*****@*****.**')
Exemplo n.º 26
0
 def test_from_id_missing(self) -> None:
     """Test exception on missing user `id`."""
     with pytest.raises(NotFound):
         User.from_id(-1)
Exemplo n.º 27
0
 def test_from_id(self, testdata: TestData) -> None:
     """Test loading user profile from `id`."""
     # NOTE: `id` not set until after insert
     for i, user in enumerate(testdata['user']):
         assert User.from_id(i + 1).alias == user['alias']
Exemplo n.º 28
0
 def test_embedded(self, testdata: TestData) -> None:
     """Tests embedded method to check JSON-serialization."""
     for data in testdata['user']:
         assert data == json_roundtrip(User(**data).to_json())
Exemplo n.º 29
0
 def test_init(self, testdata: TestData) -> None:
     """Create user instance and validate accessors."""
     for data in testdata['user']:
         user = User(**data)
         for key, value in data.items():
             assert getattr(user, key) == value
Exemplo n.º 30
0
 def test_dict(self, testdata: TestData) -> None:
     """Test round-trip of dict translations."""
     for data in testdata['user']:
         user = User.from_dict(data)
         assert data == user.to_dict()