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
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
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
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
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
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
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
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
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'
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
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' } } }
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'}
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']
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
def test_from_alias_missing(self) -> None: """Test exception on missing user `alias`.""" with pytest.raises(NotFound): User.from_alias('batman')