def test_delete_media(test_app): test_media = Game('test_name', 'test_path', 'test_genres', 'test_platform', 'test_mult', 'test-id') media_json = test_media.as_json() del media_json['_id'] with patch('media_api.media.endpoints.validate_session', return_value=ValidationResponse(MagicMock(), True, None, None)): response = test_app.delete(f'/media/{test_media.id_code}', headers={'session': '1234'}) assert response.status_code == 204
def test_search_by_type_with_param(test_app): media_json1 = Game('test_name', 'test_path', 'test_genres', 'test_platform', 'test_mult', 'test-id').as_json() media_json2 = Game('test_name1', 'test_path', 'test_genres', 'test_platform', 'test_mult', 'test-id').as_json() user = User(document={'media': [media_json1, media_json2]}) with patch('media_api.media.endpoints.validate_session', return_value=ValidationResponse(user, True, None, None)): response = test_app.get('/media/types/Game?name=test_name1', headers={'session': '1234'}) assert response.status_code == 200 assert response.json == [media_json2]
def test_create_media_bad_type(test_app): test_media = Game('test_name', 'test_path', 'test_genres', 'test_platform', 'test_mult', 'test-id') media_json = test_media.as_json() media_json['type'] = 'Frog' del media_json['_id'] with patch('media_api.media.endpoints.validate_session', return_value=ValidationResponse(MagicMock(), True, None, None)): response = test_app.post('/media', json=media_json, headers={'session': '1234'}) assert response.status_code == 400
def test_game_generated_id(): game = Game('test_name', 'test_path', 'test_genres', 'test_platform', 'test_mult') json_ = game.as_json() del json_['_id'] # this is randomly created expected_json = { 'type': 'Game', 'name': 'test_name', 'thumbnail_path': 'test_path', 'genres': 'test_genres', 'platform': 'test_platform', 'multiplayer': 'test_mult' } assert json_ == expected_json
def test_game_exact_id(): game = Game('test_name', 'test_path', 'test_genres', 'test_platform', 'test_mult', 'test-id') json_ = game.as_json() expected_json = { '_id': 'test-id', 'type': 'Game', 'name': 'test_name', 'thumbnail_path': 'test_path', 'genres': 'test_genres', 'platform': 'test_platform', 'multiplayer': 'test_mult' } assert json_ == expected_json
def test_delete_media(): test_media1 = Game('test_name', 'test_path', 'test_genres', 'test_platform', 'test_mult', 'test-id') test_media2 = Game('test_name', 'test_path', 'test_genres', 'test_platform', 'test_mult', 'test-id2') test_user = user.User({'media': [test_media1.as_json(), test_media2.as_json()], '_id': ''}) with patch.object(user, 'DB', {config.MONGODB_USER_COLLECTION_NAME: MagicMock()}): test_user.delete_media(test_media2.id_code) assert test_user.media == [test_media1.as_json()]
def test_get_media_by_id(test_app): media_json1 = Game('test_name', 'test_path', 'test_genres', 'test_platform', 'test_mult', 'test-id').as_json() user = User(document={'media': [media_json1]}) with patch('media_api.media.endpoints.validate_session', return_value=ValidationResponse(user, True, None, None)): response = test_app.get(f'/media/{media_json1["_id"]}', headers={'session': '1234'}) assert response.status_code == 200 assert response.json == media_json1
def test_read_media_error(): test_media = Game('test_name', 'test_path', 'test_genres', 'test_platform', 'test_mult', 'test-id') test_user = user.User({'media': [test_media.as_json()], '_id': ''}) with pytest.raises(ValueError): result = test_user.read_media('not an id code')
def test_read_media(): test_media1 = Game('test_name', 'test_path', 'test_genres', 'test_platform', 'test_mult', 'test-id') test_media2 = Game('test_name', 'test_path', 'test_genres', 'test_platform', 'test_mult', 'test-id2') test_user = user.User({'media': [test_media1.as_json(), test_media2.as_json()], '_id': ''}) result = test_user.read_media(test_media2.id_code) assert result == test_media2.as_json()
def test_create_media(): test_media = Game('test_name', 'test_path', 'test_genres', 'test_platform', 'test_mult', 'test-id') test_user = user.User({'media': [], '_id': ''}) with patch.object(user, 'DB', {config.MONGODB_USER_COLLECTION_NAME: MagicMock()}): test_user.create_media(test_media) assert test_user.media == [test_media.as_json()]
def test_update_media_error(): test_media = Game('test_name', 'test_path', 'test_genres', 'test_platform', 'test_mult', 'test-id') test_media2 = Game('test_name', 'test_path', 'test_genres', 'test_platform', 'test_mult', 'test-id2') test_user = user.User({'media': [test_media.as_json()], '_id': ''}) with pytest.raises(ValueError): test_user.update_media(test_media2)