def test_go_link(client, example_pub): user = UserFactory() client.force_login(user) response = client.get(reverse( 'publication:go', kwargs=dict(short_url=example_pub.short_url)), follow=True) assert response.status_code == 200 assert_in_content(response, example_pub.surface.name)
def test_link_for_sharing_info(client): password = "******" user = UserFactory(password=password) assert client.login(username=user.username, password=password) response = client.get(reverse('home')) assert response.status_code == 200 assert_in_content(response, reverse('manager:sharing-info'))
def test_warnings_for_different_arguments(client, handle_usage_statistics): user = UserFactory() surf1 = SurfaceFactory(creator=user) surf2 = SurfaceFactory(creator=user) topo1a = Topography1DFactory(surface=surf1) topo1b = Topography1DFactory(surface=surf1) topo2a = Topography1DFactory(surface=surf2) func = AnalysisFunctionFactory() topo_impl = AnalysisFunctionImplementationFactory(function=func, subject_type=topo1a.get_content_type(), code_ref='topography_analysis_function_for_tests') surf_impl = AnalysisFunctionImplementationFactory(function=func, subject_type=surf1.get_content_type(), code_ref='surface_analysis_function_for_tests') # # Generate analyses for topographies with differing arguments # kwargs_1a = pickle.dumps(dict(a=1, b=2)) kwargs_1b = pickle.dumps(dict(a=1, b=3)) # differing from kwargs_1a! ana1a = TopographyAnalysisFactory(subject=topo1a, function=func, kwargs=kwargs_1a) ana1b = TopographyAnalysisFactory(subject=topo1b, function=func, kwargs=kwargs_1b) ana2a = TopographyAnalysisFactory(subject=topo2a, function=func) # default arguments # # Generate analyses for surfaces with differing arguments # kwargs_1 = pickle.dumps(dict(a=1, c=2)) kwargs_2 = pickle.dumps(dict(a=1, c=3)) # differing from kwargs_1a! ana1 = SurfaceAnalysisFactory(subject=surf1, function=func, kwargs=kwargs_1) ana2 = SurfaceAnalysisFactory(subject=surf2, function=func, kwargs=kwargs_2) client.force_login(user) # # request card, there should be warnings, one for topographies and one for surfaces # response = client.post(reverse("analysis:card"), data={ 'subjects_ids_json': subjects_to_json([topo1a, topo1b, topo2a, surf1, surf2]), 'function_id': func.id, 'card_id': "card-1", 'template_flavor': 'list' }, HTTP_X_REQUESTED_WITH='XMLHttpRequest', follow=True) assert response.status_code == 200 assert_in_content(response, "Arguments for this analysis function differ among chosen " "subjects of type 'manager | topography'") assert_in_content(response, "Arguments for this analysis function differ among chosen " "subjects of type 'manager | surface'")
def test_terms_conditions_as_anonymous(client): # Install terms and conditions for test TermsAndConditions.objects.create(slug='test-terms', name="Test of T&C", text="some text", date_active=timezone.now()) response = client.get(reverse('terms')) assert_in_content(response, "Test of T&C") response = client.get( reverse('tc_accept_specific_page', kwargs=dict(slug='test-terms'))) assert_in_content(response, "some text")
def test_anonymous_user_can_see_published(client, handle_usage_statistics): # # publish a surface # bob = UserFactory(name="Bob") surface_name = "Diamond Structure" surface = SurfaceFactory(creator=bob, name=surface_name) topo = Topography1DFactory(surface=surface) pub = surface.publish('cc0-1.0', bob.name) # no one is logged in now, assuming the select tab sends a search request response = client.get(reverse('manager:search')) # should see the published surface assert_in_content(response, surface_name)
def test_share_surface_through_UI(client, handle_usage_statistics): user1 = UserFactory() user2 = UserFactory() surface = SurfaceFactory(creator=user1) assert not user2.has_perm('view_surface', surface) assert not user2.has_perm('change_surface', surface) assert not user2.has_perm('delete_surface', surface) assert not user2.has_perm('share_surface', surface) client.force_login(user1) response = client.get(reverse("manager:surface-detail", kwargs=dict(pk=surface.pk))) share_link = reverse("manager:surface-share", kwargs=dict(pk=surface.pk)) assert_in_content(response, share_link) response = client.get(share_link) assert_in_content(response, "Share this surface") response = client.post(share_link, { 'save': 'save', 'users': [user2.id], 'allow_change': True }, follow=True) assert response.status_code == 200 # # Now the surface should be shared # assert user2.has_perm('view_surface', surface) assert user2.has_perm('change_surface', surface) # still no delete or change allowed assert not user2.has_perm('delete_surface', surface) assert not user2.has_perm('share_surface', surface) # # There are notifications for user 2 # assert Notification.objects.filter(recipient=user2, verb='share', description__contains=surface.name).count() == 1 assert Notification.objects.filter(recipient=user2, verb='allow change', description__contains=surface.name).count() == 1
def test_dont_show_published_surfaces_on_sharing_info(client): alice = UserFactory() bob = UserFactory() surface1 = SurfaceFactory(creator=alice, name="Shared Surface") surface1.share(bob) surface2 = SurfaceFactory(creator=alice, name="Published Surface") surface2.publish('cc0-1.0', 'Alice') # # Login as Bob, surface 1 should be listed on sharing info page # and same for alice # for user in [alice, bob]: client.force_login(user) response = client.get(reverse('manager:sharing-info')) assert_in_content(response, "Shared Surface") assert_not_in_content(response, "Published Surface") client.logout()
def test_go_download_link(client, example_pub, handle_usage_statistics): user = UserFactory() client.force_login(user) response = client.get(reverse( 'publication:go-download', kwargs=dict(short_url=example_pub.short_url)), follow=True) assert response.status_code == 200 surface = example_pub.surface # open zip file and look into meta file, there should be two surfaces and three topographies with zipfile.ZipFile(BytesIO(response.content)) as zf: meta_file = zf.open('meta.yml') meta = yaml.safe_load(meta_file) assert len(meta['surfaces']) == 1 assert len( meta['surfaces'][0]['topographies']) == surface.num_topographies() assert meta['surfaces'][0]['name'] == surface.name assert_in_content(response, example_pub.surface.name)
def test_analysis_times(client, two_topos, django_user_model, handle_usage_statistics): user = django_user_model.objects.get(username='******') client.force_login(user) topo = Topography.objects.first() af = AnalysisFunction.objects.first() pickled_result = pickle.dumps({'name': 'test function', 'xlabel': 'x', 'ylabel': 'y', 'xunit': '1', 'yunit': '1', 'series': [], }) analysis = TopographyAnalysisFactory.create( subject=topo, function=af, task_state=Analysis.SUCCESS, start_time=datetime.datetime(2018, 1, 1, 12), end_time=datetime.datetime(2018, 1, 1, 13, 1, 1), # duration: 1 hour, 1 minute, 1 sec result=pickled_result, ) analysis.users.add(user) analysis.save() response = client.post(reverse("analysis:card"), data={ 'subjects_ids_json': subjects_to_json([topo]), 'function_id': af.id, 'card_id': "card-1", 'template_flavor': 'list', }, HTTP_X_REQUESTED_WITH='XMLHttpRequest', follow=True) assert response.status_code == 200 assert_in_content(response, "2018-01-01 12:00:00") # start_time assert_in_content(response, "1:01:01") # duration
def test_notification_saying_new_version_exists(client, settings, handle_usage_statistics): settings.MIN_SECONDS_BETWEEN_SAME_SURFACE_PUBLICATIONS = None user = UserFactory() surface = SurfaceFactory(creator=user) topo1 = Topography2DFactory(surface=surface) topo2 = Topography2DFactory(surface=surface) client.force_login(user) # # Now publish two times # pub1 = surface.publish('cc0-1.0', 'Alice') pub2 = surface.publish('cc0-1.0', 'Alice') # # When showing page for "Work in Progress" surface, there should be a hint there are publications # response = client.get( reverse('manager:surface-detail', kwargs=dict(pk=surface.pk))) assert_in_content(response, "Published versions available") # # When showing page for first publication, there should be a hint there's a newer version # response = client.get( reverse('manager:surface-detail', kwargs=dict(pk=pub1.surface.pk))) assert_in_content(response, "Newer version available") # # When showing page for second publication, there shouldn't be such a notice # response = client.get( reverse('manager:surface-detail', kwargs=dict(pk=pub2.surface.pk))) assert_not_in_content(response, "Newer version available")
def test_dont_show_published_surfaces_when_shared_filter_used( client, handle_usage_statistics): alice = UserFactory() bob = UserFactory() surface1 = SurfaceFactory(creator=alice, name="Shared Surface") surface1.share(bob) surface2 = SurfaceFactory(creator=alice, name="Published Surface") surface2.publish('cc0-1.0', 'Alice') client.force_login(bob) response = client.get( reverse('manager:search') + '?sharing_status=shared') # means "shared with you" assert_in_content(response, "Shared Surface") assert_not_in_content(response, "Published Surface") response = client.get( reverse('manager:search') + '?sharing_status=published') # means "published by anyone" assert_not_in_content(response, "Shared Surface") assert_in_content(response, "Published Surface")
def test_switch_versions_on_properties_tab(client, settings, handle_usage_statistics): settings.MIN_SECONDS_BETWEEN_SAME_SURFACE_PUBLICATIONS = None user = UserFactory() surface = SurfaceFactory(creator=user) topo1 = Topography2DFactory(surface=surface) topo2 = Topography2DFactory(surface=surface) # # First: The surface is not published yet # client.force_login(user) response = client.get( reverse('manager:surface-detail', kwargs=dict(pk=surface.pk))) assert response.status_code == 200 assert_not_in_content(response, 'Version 1') assert_not_in_content(response, 'Version 2') # # Now publish the first time # publication = surface.publish('cc0-1.0', 'Alice') assert publication.version == 1 assert publication.license == 'cc0-1.0' assert publication.original_surface == surface pub_date_1 = publication.datetime.date() response = client.get( reverse('manager:surface-detail', kwargs=dict(pk=surface.pk))) assert response.status_code == 200 assert_in_content(response, 'Version 1 ({})'.format(pub_date_1)) assert_not_in_content(response, 'Version 2') # # Publish again # publication = surface.publish('cc0-1.0', 'Alice') assert publication.version == 2 assert publication.original_surface == surface pub_date_2 = publication.datetime.date() response = client.get( reverse('manager:surface-detail', kwargs=dict(pk=surface.pk))) assert response.status_code == 200 assert_in_content(response, 'Version 1 ({})'.format(pub_date_1)) assert_in_content(response, 'Version 2 ({})'.format(pub_date_2))
def test_error_message_when_topography_file_cannot_be_loaded( client, topography_loaded_from_broken_file, handle_usage_statistics): client.force_login(user=topography_loaded_from_broken_file.surface.creator) response = client.get( reverse('manager:topography-detail', kwargs=dict(pk=topography_loaded_from_broken_file.pk))) # there should be no internal server error assert response.status_code == 200 # there should be an error message showing that the topography could not be loaded assert_in_content(response, f"{topography_loaded_from_broken_file.name}") assert_in_content( response, f"(id: {topography_loaded_from_broken_file.id}) cannot be loaded unexpectedly." ) assert_in_content(response, "send us an e-mail about this issue")
def test_show_license_when_viewing_published_surface(rf, settings): license = 'cc0-1.0' surface = SurfaceFactory() pub = surface.publish(license, 'Mike Publisher') request = rf.get( reverse('manager:surface-detail', kwargs=dict(pk=pub.surface.pk))) request.user = surface.creator request.session = {} response = SurfaceDetailView.as_view()(request, pk=pub.surface.pk) response.render() license_info = settings.CC_LICENSE_INFOS[license] assert_in_content(response, license_info['title']) assert_in_content(response, license_info['description_url']) assert_in_content(response, license_info['legal_code_url'])
def test_bandwidth_error_message_in_UI_when_problems_while_loading( client, topography_loaded_from_broken_file, handle_usage_statistics): # # Theoretically loading of a topography can fail during # creation of the bandwidth plot, although it worked before. # This can happen e.g. because of an update of the SurfaceTopography package # which may introduce new errors. # In this case the user should see an error message in the UI. # surface = topography_loaded_from_broken_file.surface user = surface.creator client.force_login(user=user) response = client.get( reverse('manager:surface-detail', kwargs=dict(pk=surface.pk))) assert response.status_code == 200 assert_in_content(response, f"{topography_loaded_from_broken_file.name}") assert_in_content(response, "is not yet available.") assert_in_content(response, "send us an e-mail about this issue")
def test_download_plot_analyses_to_txt(rf): func = AnalysisFunctionFactory() impl = AnalysisFunctionImplementationFactory(function=func) analysis = TopographyAnalysisFactory(function=func) request = rf.get( reverse('analysis:download', kwargs=dict(ids=str(analysis.id), card_view_flavor='plot', file_format='txt'))) response = download_plot_analyses_to_txt(request, [analysis]) assert_in_content(response, 'Fibonacci') assert_in_content( response, '1.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00' ) assert_in_content( response, '8.000000000000000000e+00 1.300000000000000000e+01 0.000000000000000000e+00' )
def test_renewal_on_topography_creation(client, mocker, handle_usage_statistics, django_capture_on_commit_callbacks): renew_topo_analyses_mock = mocker.patch( 'topobank.manager.views.renew_analyses_related_to_topography.si') renew_topo_images_mock = mocker.patch( 'topobank.manager.views.renew_topography_images.si') user = UserFactory() surface = SurfaceFactory(creator=user) client.force_login(user) # # open first step of wizard: file upload # input_file_path = Path( FIXTURE_DIR + '/example-2d.npy') # maybe use package 'pytest-datafiles' here instead with open(str(input_file_path), mode='rb') as fp: response = client.post(reverse('manager:topography-create', kwargs=dict(surface_id=surface.id)), data={ 'topography_create_wizard-current_step': 'upload', 'upload-datafile': fp, 'upload-datafile_format': '', 'upload-surface': surface.id, }, follow=True) assert response.status_code == 200 assert_no_form_errors(response) # # now we should be on the page with second step # assert_in_content(response, "Step 2 of 3") assert_in_content(response, '<option value="0">Default</option>') assert response.context['form'].initial['name'] == 'example-2d.npy' # # Send data for second page # response = client.post(reverse('manager:topography-create', kwargs=dict(surface_id=surface.id)), data={ 'topography_create_wizard-current_step': 'metadata', 'metadata-name': 'topo1', 'metadata-measurement_date': '2020-10-21', 'metadata-data_source': 0, 'metadata-description': "description", }, follow=True) assert_no_form_errors(response) # # Send data for third page # assert_in_content(response, "Step 3 of 3") with django_capture_on_commit_callbacks(execute=True) as callbacks: response = client.post( reverse('manager:topography-create', kwargs=dict(surface_id=surface.id)), data={ 'topography_create_wizard-current_step': 'units', 'units-size_x': '1', 'units-size_y': '1', 'units-unit': 'nm', 'units-height_scale': 1, 'units-detrend_mode': 'height', 'units-resolution_x': 2, 'units-resolution_y': 2, 'units-instrument_type': 'undefined', 'units-has_undefined_data': False, 'units-fill_undefined_data_mode': Topography.FILL_UNDEFINED_DATA_MODE_NOFILLING, }, follow=True) assert_no_form_errors(response) assert len( callbacks ) == 1 # single chain for squeezed file, thumbnail and for analyses assert renew_topo_analyses_mock.called assert renew_topo_images_mock.called
def test_appearance_buttons_based_on_permissions(client, handle_usage_statistics): password = "******" user1 = UserFactory(password=password) user2 = UserFactory(password=password) surface = SurfaceFactory(creator=user1) surface_detail_url = reverse('manager:surface-detail', kwargs=dict(pk=surface.pk)) surface_share_url = reverse('manager:surface-share', kwargs=dict(pk=surface.pk)) surface_update_url = reverse('manager:surface-update', kwargs=dict(pk=surface.pk)) surface_delete_url = reverse('manager:surface-delete', kwargs=dict(pk=surface.pk)) surface.share(user2) topo = Topography2DFactory(surface=surface, size_y=512) topo_detail_url = reverse('manager:topography-detail', kwargs=dict(pk=topo.pk)) topo_update_url = reverse('manager:topography-update', kwargs=dict(pk=topo.pk)) topo_delete_url = reverse('manager:topography-delete', kwargs=dict(pk=topo.pk)) # # first user can see links for editing and deletion # assert client.login(username=user1.username, password=password) response = client.get(surface_detail_url) assert_in_content(response, surface_share_url) assert_in_content(response, surface_update_url) assert_in_content(response, surface_delete_url) response = client.get(topo_detail_url) assert_in_content(response, topo_update_url) assert_in_content(response, topo_delete_url) client.logout() # # Second user can't see those links, only view stuff # assert client.login(username=user2.username, password=password) response = client.get(surface_detail_url) assert_not_in_content(response, surface_share_url) assert_not_in_content(response, surface_update_url) assert_not_in_content(response, surface_delete_url) response = client.get(topo_detail_url) assert_not_in_content(response, topo_update_url) assert_not_in_content(response, topo_delete_url) # # When allowing to change, the second user should see links # for edit as well, for topography also "delete" and "add" # surface.share(user2, allow_change=True) response = client.get(surface_detail_url) assert_not_in_content(response, surface_share_url) # still not share assert_in_content(response, surface_update_url) assert_not_in_content(response, surface_delete_url) # still not delete response = client.get(topo_detail_url) assert_in_content(response, topo_update_url) assert_in_content(response, topo_delete_url)
def test_show_analyses_with_different_arguments(client, two_topos, django_user_model, handle_usage_statistics): user = django_user_model.objects.get(username='******') client.force_login(user) topo1 = Topography.objects.first() af = AnalysisFunction.objects.first() pickled_result = pickle.dumps({'name': 'test function', 'xlabel': 'x', 'ylabel': 'y', 'xunit': '1', 'yunit': '1', 'series': [], }) # # Create analyses for same function and topography but with different arguments # analysis = TopographyAnalysisFactory.create( subject=topo1, function=af, task_state=Analysis.SUCCESS, kwargs=pickle.dumps({'bins': 10}), start_time=datetime.datetime(2018, 1, 1, 12), end_time=datetime.datetime(2018, 1, 1, 13, 1, 1), result=pickled_result, ) analysis.users.add(user) analysis.save() # save a second, which has a later start time analysis = TopographyAnalysisFactory.create( subject=topo1, function=af, task_state=Analysis.SUCCESS, kwargs=pickle.dumps({'bins': 20}), start_time=datetime.datetime(2018, 1, 2, 12), end_time=datetime.datetime(2018, 1, 2, 13, 1, 1), result=pickled_result, ) analysis.users.add(user) analysis.save() # save a third, which has a later start time analysis = TopographyAnalysisFactory.create( subject=topo1, function=af, task_state=Analysis.SUCCESS, kwargs=pickle.dumps({'bins': 30}), start_time=datetime.datetime(2018, 1, 3, 12), end_time=datetime.datetime(2018, 1, 3, 13, 1, 1), result=pickled_result, ) analysis.users.add(user) analysis.save() # # Check response, all three analyses should be shown # response = client.post(reverse("analysis:card"), data={ 'subjects_ids_json': subjects_to_json([topo1]), 'function_id': af.id, 'card_id': "card-1", 'template_flavor': 'list' }, HTTP_X_REQUESTED_WITH='XMLHttpRequest', follow=True) assert response.status_code == 200 assert_in_content(response, "2018-01-01 12:00:00") assert_in_content(response, "2018-01-02 12:00:00") assert_in_content(response, "2018-01-03 12:00:00") # arguments should be visible in output import html.parser unescaped = html.unescape(response.content.decode()) assert str(dict(bins=10)) in unescaped assert str(dict(bins=20)) in unescaped
def test_show_multiple_analyses_for_two_functions(client, two_topos): username = '******' password = '******' assert client.login(username=username, password=password) topo1 = Topography.objects.first() topo2 = Topography.objects.last() af1 = AnalysisFunction.objects.first() af2 = AnalysisFunction.objects.last() assert topo1 != topo2 assert af1 != af2 # # Create analyses for two functions and two different topographies # counter = 0 for af in [af1, af2]: for topo in [topo1, topo2]: counter += 1 analysis = TopographyAnalysisFactory.create( subject=topo, function=af, task_state=Analysis.SUCCESS, kwargs=pickle.dumps({'bins': 10}), start_time=datetime.datetime(2018, 1, 1, counter), end_time=datetime.datetime(2018, 1, 1, counter + 1), ) analysis.save() # # Select both topographies # client.post(reverse("manager:topography-select", kwargs=dict(pk=topo1.pk))) client.post(reverse("manager:topography-select", kwargs=dict(pk=topo2.pk))) # # Check response when selecting only first function, both analyses should be shown # response = client.post(reverse("analysis:list"), data={ 'functions': [af1.id], }, follow=True) assert response.status_code == 200 assert_in_content(response, "Example 3 - ZSensor") assert_in_content(response, "Example 4 - Default") # # Check response when selecting only both functions, both analyses should be shown # response = client.post(reverse("analysis:list"), data={ 'functions': [af1.id, af2.id], }, follow=True) assert response.status_code == 200 assert_in_content(response, "Example 3 - ZSensor") assert_in_content(response, "Example 4 - Default")
def test_anonymous_user_only_published_as_default(client): response = client.get(reverse('manager:select')) assert_not_in_content(response, 'All accessible surfaces') assert_not_in_content(response, 'Only own surfaces') assert_not_in_content(response, 'Only surfaces shared with you') assert_in_content(response, 'Only surfaces published by anyone')
def test_welcome_page_statistics(client, test_instances, with_publication, handle_usage_statistics): (user_1, user_2), (surface_1, surface_2), (topography_1, ) = test_instances surface_2.share(user_2) if with_publication: surface_1.publish('cc0-1.0', 'Issac Newton') # # Test statistics if user is not yet authenticated # response = client.get(reverse('home')) assert_in_content( response, '<div class="welcome-page-statistics">2</div> registered users') assert_in_content( response, '<div class="welcome-page-statistics">2</div> digital surface twins') assert_in_content( response, '<div class="welcome-page-statistics">1</div> individual measurements') assert_in_content( response, '<div class="welcome-page-statistics">1</div> computed analyses') # # Test statistics if user_1 is authenticated # client.force_login(user_1) response = client.get(reverse('home')) assert_in_content( response, '<div class="welcome-page-statistics">2</div> digital surface twins') assert_in_content( response, '<div class="welcome-page-statistics">1</div> individual measurements') assert_in_content( response, '<div class="welcome-page-statistics">1</div> computed analyses') assert_in_content( response, '<div class="welcome-page-statistics">0</div> digital twins of other users' ) client.logout() # # Test statistics if user_2 is authenticated # client.force_login(user_2) response = client.get(reverse('home')) assert_in_content( response, '<div class="welcome-page-statistics">0</div> digital surface twins') assert_in_content( response, '<div class="welcome-page-statistics">0</div> individual measurements') assert_in_content( response, '<div class="welcome-page-statistics">0</div> computed analyses') if with_publication: num_access = 2 else: num_access = 1 assert_in_content( response, f'<div class="welcome-page-statistics">{num_access}</div> digital twins of other users' ) client.logout()
def test_view_shared_analysis_results(client, handle_usage_statistics): password = '******' # # create database objects # user1 = UserFactory(password=password) user2 = UserFactory(password=password) surface1 = SurfaceFactory(creator=user1) surface2 = SurfaceFactory(creator=user2) # create topographies + functions + analyses func1 = AnalysisFunctionFactory() impl1 = AnalysisFunctionImplementationFactory(function=func1) # func2 = AnalysisFunctionFactory() # Two topographies for surface1 topo1a = Topography1DFactory(surface=surface1, name='topo1a') topo1b = Topography1DFactory(surface=surface1, name='topo1b') # One topography for surface2 topo2a = Topography1DFactory(surface=surface2, name='topo2a') # analyses, differentiate by start time analysis1a_1 = TopographyAnalysisFactory(subject=topo1a, function=func1, start_time=datetime.datetime(2019, 1, 1, 12)) analysis1b_1 = TopographyAnalysisFactory(subject=topo1b, function=func1, start_time=datetime.datetime(2019, 1, 1, 13)) analysis2a_1 = TopographyAnalysisFactory(subject=topo2a, function=func1, start_time=datetime.datetime(2019, 1, 1, 14)) # Function should have three analyses, all successful (the default when using the factory) assert func1.analysis_set.count() == 3 assert all(a.task_state == 'su' for a in func1.analysis_set.all()) # user2 shares surfaces, so user 1 should see surface1+surface2 surface2.share(user1) # # Now we change to the analysis card view and look what we get # assert client.login(username=user1.username, password=password) response = client.post(reverse("analysis:card"), data={ 'subjects_ids_json': subjects_to_json([topo1a, topo1b, topo2a]), 'function_id': func1.id, 'card_id': 1, 'template_flavor': 'list' }, HTTP_X_REQUESTED_WITH='XMLHttpRequest', follow=True) # Function should still have three analyses, all successful (the default when using the factory) assert func1.analysis_set.count() == 3 assert all(a.task_state == 'su' for a in func1.analysis_set.all()) assert response.status_code == 200 # We should see start times of all three topographies assert_in_content(response, '2019-01-01 12:00:00') # topo1a assert_in_content(response, '2019-01-01 13:00:00') # topo1b assert_in_content(response, '2019-01-01 14:00:00') # topo2a client.logout() # # user 2 cannot access results from topo1, it is not shared # assert client.login(username=user2.username, password=password) response = client.post(reverse("analysis:card"), data={ 'subjects_ids_json': subjects_to_json([topo1a, topo1b, topo2a]), 'function_id': func1.id, 'card_id': 1, 'template_flavor': 'list' }, HTTP_X_REQUESTED_WITH='XMLHttpRequest', follow=True) assert response.status_code == 200 assert_not_in_content(response, '2019-01-01 12:00:00') # topo1a assert_not_in_content(response, '2019-01-01 13:00:00') # topo1b assert_in_content(response, '2019-01-01 14:00:00') # topo2a client.logout()
def test_list_surface_permissions(client, handle_usage_statistics): # # create database objects # password = '******' user1 = UserFactory(password=password) user2 = UserFactory(name="Bob Marley") user3 = UserFactory(name="Alice Cooper") surface = SurfaceFactory(creator=user1) surface.share(user2) surface.share(user3, allow_change=True) surface_detail_url = reverse('manager:surface-detail', kwargs=dict(pk=surface.pk)) # # now user 1 has access to surface detail page # assert client.login(username=user1.username, password=password) response = client.get(surface_detail_url) assert_in_content(response, "Permissions") # related to user 1 assert_in_content(response, "You have the permission to share this surface") assert_in_content(response, "You have the permission to delete this surface") assert_in_content(response, "You have the permission to change this surface") assert_in_content(response, "You have the permission to view this surface") # related to user 2 assert_in_content(response, "Bob Marley hasn't the permission to share this surface") assert_in_content(response, "Bob Marley hasn't the permission to delete this surface") assert_in_content(response, "Bob Marley hasn't the permission to change this surface") assert_in_content(response, "Bob Marley has the permission to view this surface") # related to user 3 assert_in_content(response, "Alice Cooper hasn't the permission to share this surface") assert_in_content(response, "Alice Cooper hasn't the permission to delete this surface") assert_in_content(response, "Alice Cooper has the permission to change this surface") assert_in_content(response, "Alice Cooper has the permission to view this surface")
def test_upload_topography_for_shared_surface(client, handle_usage_statistics): input_file_path = Path(FIXTURE_DIR+'/example3.di') description = "test description" password = '******' user1 = UserFactory(password=password) user2 = UserFactory(password=password) surface = SurfaceFactory(creator=user1) surface.share(user2) # first without allowing change assert client.login(username=user2.username, password=password) # # open first step of wizard: file upload # with open(str(input_file_path), mode='rb') as fp: response = client.post(reverse('manager:topography-create', kwargs=dict(surface_id=surface.id)), data={ 'topography_create_wizard-current_step': 'upload', 'upload-datafile': fp, 'upload-surface': surface.id, }, follow=True) assert response.status_code == 403 # user2 is not allowed to change # # Now allow to change and get response again # surface.share(user2, allow_change=True) with open(str(input_file_path), mode='rb') as fp: response = client.post(reverse('manager:topography-create', kwargs=dict(surface_id=surface.id)), data={ 'topography_create_wizard-current_step': 'upload', 'upload-datafile': fp, 'upload-surface': surface.id, }, follow=True) assert response.status_code == 200 # # check contents of second page # # now we should be on the page with second step assert b"Step 2 of 3" in response.content, "Errors:"+str(response.context['form'].errors) # we should have two datasources as options, "ZSensor" and "Height" assert b'<option value="0">ZSensor</option>' in response.content assert b'<option value="3">Height</option>' in response.content assert response.context['form'].initial['name'] == 'example3.di' # # Send data for second page # response = client.post(reverse('manager:topography-create', kwargs=dict(surface_id=surface.id)), data={ 'topography_create_wizard-current_step': 'metadata', 'metadata-name': 'topo1', 'metadata-measurement_date': '2018-06-21', 'metadata-data_source': 0, 'metadata-description': description, }) assert response.status_code == 200 assert b"Step 3 of 3" in response.content, "Errors:" + str(response.context['form'].errors) # # Send data for third page # response = client.post(reverse('manager:topography-create', kwargs=dict(surface_id=surface.id)), data={ 'topography_create_wizard-current_step': 'units', 'units-size_x': '9000', 'units-size_y': '9000', 'units-unit': 'nm', 'units-height_scale': 0.3, 'units-detrend_mode': 'height', 'units-resolution_x': 256, 'units-resolution_y': 256, 'units-instrument_type': Topography.INSTRUMENT_TYPE_UNDEFINED, 'units-fill_undefined_data_mode': Topography.FILL_UNDEFINED_DATA_MODE_NOFILLING, }, follow=True) assert response.status_code == 200 # assert reverse('manager:topography-detail', kwargs=dict(pk=1)) == response.url # export_reponse_as_html(response) assert 'form' not in response.context, "Errors:" + str(response.context['form'].errors) topos = surface.topography_set.all() assert len(topos) == 1 t = topos[0] assert t.measurement_date == datetime.date(2018,6,21) assert t.description == description assert "example3" in t.datafile.name assert 256 == t.resolution_x assert 256 == t.resolution_y assert t.creator == user2 # # Test little badge which shows who uploaded data # response = client.get(reverse('manager:topography-detail', kwargs=dict(pk=t.pk))) assert response.status_code == 200 assert_in_content(response, 'uploaded by you') client.logout() assert client.login(username=user1.username, password=password) response = client.get(reverse('manager:topography-detail', kwargs=dict(pk=t.pk))) assert response.status_code == 200 assert_in_content(response, 'uploaded by {}'.format(user2.name)) client.logout() # # There should be a notification of the user # exp_mesg = f"User '{user2.name}' has created the topography '{t.name}' "+\ f"in surface '{t.surface.name}'." assert Notification.objects.filter(unread=True, recipient=user1, verb='create', description__contains=exp_mesg).count() == 1