def setUp(self): self.username, self.password = self.create_user('admin', 'admin', is_superuser=True) self.user = get_user_model().objects.filter(username='******')[0] self.username, self.password = self.create_user('admin', 'admin', is_superuser=True) admin_client = AdminClient() admin_client.login_as_admin() response = admin_client.get( reverse('profile_detail', args=[self.username])) # Test for final status code = HTTP OK response = admin_client.get( reverse('profile_detail', args=[self.username])) self.assertTemplateUsed(response, 'people/profile_detail.html') # Should have all the tab names shown self.assertContains(response, 'Stories') self.assertContains(response, 'Layers') self.assertContains(response, 'Uploads') self.assertContains(response, 'Messages') self.assertContains(response, 'Activity Feed') self.assertContains(response, 'Journal Entries') self.assertContains(response, 'Favorites') self.assertContains(response, 'Icons') self.assertContains(response, 'Stories')
def setUp(self): self.username, self.password = self.create_user( 'admin', 'admin', is_superuser=True) self.user = get_user_model().objects.filter(username='******')[0] self.username, self.password = self.create_user( 'admin', 'admin', is_superuser=True) admin_client = AdminClient() admin_client.login_as_admin() response = admin_client.get( reverse('profile_detail', args=[self.username])) # Test for final status code = HTTP OK response = admin_client.get( reverse('profile_detail', args=[self.username])) self.assertTemplateUsed(response, 'people/profile_detail.html') # Should have all the tab names shown self.assertContains(response, 'Stories') self.assertContains(response, 'Layers') self.assertContains(response, 'Uploads') self.assertContains(response, 'Messages') self.assertContains(response, 'Activity Feed') self.assertContains(response, 'Journal Entries') self.assertContains(response, 'Favorites') self.assertContains(response, 'Icons') self.assertContains(response, 'Stories')
def test_layer(self): layer = Layer.objects.first() c = AdminClient() c.login_as_admin() response = c.get(reverse('layer_detail', args=[layer.typename])) self.assertEqual(response.status_code, 200) response = c.get(reverse('layer_metadata', args=[layer.typename])) self.assertEqual(response.status_code, 200)
def test_layer(self): layer = Layer.objects.first() c = AdminClient() c.login_as_admin() response = c.get(reverse('layer_detail', args=[layer.typename])) self.assertEqual(response.status_code, 200) response = c.get(reverse('layer_metadata', args=[layer.typename])) self.assertEqual(response.status_code, 200)
def test_manager_post_social_media_changes(self): # Create an organization without social media. org = get_test_organization() # Check the details page for links. response = self.client.get(org.get_absolute_url()) self.assertEqual(200, response.status_code) self.assertContains(response, org.name) management_url = reverse( "organizations:manage", kwargs={'slug': org.slug}) # Try to manage anonymously and get denied response = self.client.get(management_url, follow=True) self.assertEqual(200, response.status_code) self.assertContains(response, "Log in to an existing account") # Try to post anonymously and get denied response = self.client.post( management_url, {'facebook': 'unodostre'}, follow=True ) self.assertEqual(200, response.status_code) self.assertContains(response, "Log in to an existing account") # Create user without permissions non_admin_user = User.objects.create_user( username='******', email='*****@*****.**', password='******' ) non_admin_user.save() # Login as c = AdminClient() c.login_as_admin() # Should not get the management button response = c.get(org.get_absolute_url()) self.assertNotContains(response, "MANAGE") # Try to manage as someone who is not and admin and get denied response = c.get(reverse("organizations:manage", kwargs={ 'slug': org.slug}), follow=True) self.assertEqual(200, response.status_code) self.assertTemplateUsed("organiazation_detail.html") # Try to post changes as someone who is not admin and get denied response = c.post(management_url, { 'facebook': 'onetwothree' }, follow=True) self.assertEqual(200, response.status_code) self.assertTemplateUsed("organiazation_detail.html")
def test_manager_post_social_media_changes(self): # Create an organization without social media. org = get_test_organization() # Check the details page for links. response = self.client.get(org.get_absolute_url()) self.assertEqual(200, response.status_code) self.assertContains(response, org.title) management_url = reverse("organizations:manage", kwargs={'slug': org.slug}) # Try to manage anonymously and get denied response = self.client.get(management_url, follow=True) self.assertEqual(200, response.status_code) self.assertContains(response, "Log in to an existing account") # Try to post anonymously and get denied response = self.client.post(management_url, {'facebook': 'unodostre'}, follow=True) self.assertEqual(200, response.status_code) self.assertContains(response, "Log in to an existing account") # Create user without permissions non_admin_user = User.objects.create_user( username='******', email='*****@*****.**', password='******') non_admin_user.save() # Login as c = AdminClient() c.login_as_admin() # Should not get the management button response = c.get(org.get_absolute_url()) self.assertNotContains(response, "MANAGE") # Try to manage as someone who is not and admin and get denied response = c.get(reverse("organizations:manage", kwargs={'slug': org.slug}), follow=True) self.assertEqual(200, response.status_code) self.assertTemplateUsed("organiazation_detail.html") # Try to post changes as someone who is not admin and get denied response = c.post(management_url, {'facebook': 'onetwothree'}, follow=True) self.assertEqual(200, response.status_code) self.assertTemplateUsed("organiazation_detail.html")
class HealthCheckViewTest(MapStoryTestMixin): def setUp(self): self.test_username, self.test_password = self.create_user('testHealthCheck', 'testHealthCheck') self.userclient = AdminClient() def tearDown(self): pass def create_user(self, username, password, **kwargs): """ Convenience method for creating users. """ user, created = get_user_model().objects.get_or_create(username=username, **kwargs) if created: user.set_password(password) user.save() return username, password @skip("This works locally. Skipping until geoserver works.") def test_template(self): self.create_user(username='******', password='******', is_superuser=True) self.userclient.login_as_non_admin('test_admin', 'test_admin') response = self.userclient.get(reverse('health_check'), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'health_check/dashboard.html')
def test_access_with_admin_to_manager(self): c = AdminClient() c.login_as_admin() org = get_test_organization() r = c.get(reverse("organizations:manage", kwargs={'slug': org.slug}), follow=True) self.assertEqual(200, r.status_code)
def test_member_404(self): admin_client = AdminClient() admin_client.login_as_non_admin() response = admin_client.get( reverse('organization_members', args=['Test-Organization-nonexistent'])) self.assertEqual(response.status_code, 404)
def test_organization_members_add(self): """Organization member add url(r'^organizations/(?P<slug>[^/]*)/members_add/$', organization_members_add, name='organization_members_add'), """ admin = AdminClient() admin.login_as_admin() # Should reject GET requests response = admin.get(reverse('organization_members_add', args=['Test-Organization']), follow=True) self.assertEqual(response.status_code, 405) # TODO: Put good data in here form_data = {} # Should accept POST requests response = admin.post(reverse('organization_members_add', args=['Test-Organization']), follow=True, data=form_data) self.assertEqual(response.status_code, 200) # Should end up in 'organization_members' last_url, status_code = response.redirect_chain[-1] self.assertRedirects(response, reverse('organization_detail', args=['Test-Organization']), status_code=302, target_status_code=200)
def test_access_with_admin_to_manager(self): c = AdminClient() c.login_as_admin() org = get_test_organization() r = c.get(reverse("organizations:manage", kwargs={ 'slug': org.slug}), follow=True) self.assertEqual(200, r.status_code)
def test_admin_access(self): """Admin should get access """ admin_client = AdminClient() admin_client.login_as_admin() response = admin_client.get(reverse('organization_create')) self.assertEqual(response.status_code, 200) self.assertHasGoogleAnalytics(response)
def test_initiatives(self): admin = AdminClient() admin.login_as_admin() response = admin.get(reverse('initiative_create')) self.assertEqual(response.status_code, 200) self.assertHasGoogleAnalytics(response) # Create new organization form_data = { 'social_twitter': 'notreal', 'social_facebook': 'notreal', 'title': 'Test Initiative', 'description': 'Testing', 'email': '*****@*****.**', 'access': 'public', 'date_joined': datetime.now(), 'city': 'Victoria', 'country': 'CAN', 'keywords': 'test', 'profile_type': 'ini', 'slug': 'Test-Initiative' } response = admin.post(reverse('initiative_create'), data=form_data) # Redirect when form is submitted, therefore 302 self.assertEqual(response.status_code, 302) # When the organization is created, a GroupProfile and Collection model pointing to it should be created group = GroupProfile.objects.all().first() collection = Collection.objects.all().first() self.assertEqual(collection.group, group) # Test editing the organization form_data = { 'title': 'Test Initiative', 'description': 'Edit', 'keywords': 'edit', 'profile_type': 'ini', 'access': 'public', 'slug': 'Test-Initiative', 'date_joined': datetime.now() } response = admin.post(reverse('initiative_edit', args=[group.slug]), data=form_data) # Redirect when form is submitted, therefore 302 self.assertEqual(response.status_code, 302) group = GroupProfile.objects.all().first() self.assertEqual(group.description, 'Edit') group_keywords = [] for keyword in group.keywords.all(): group_keywords.append(keyword.name) self.assertEqual(group_keywords, ['edit']) # Make sure the detail page can be viewed by a regular user c = Client() response = c.get(reverse('initiative_detail', args=[group.slug])) self.assertEqual(response.status_code, 200)
def test_organization_create_get(self): """Should return an empty form. """ admin_client = AdminClient() admin_client.login_as_admin() response = admin_client.get(reverse('organization_create'), follow=True) self.assertEqual(response.status_code, 200)
def test_organization_members(self): """Organization members url(r'^organizations/members/(?P<slug>[^/]*)$', organization_members, name='organization_members'), """ admin = AdminClient() admin.login_as_admin() response = admin.get(reverse('organization_members', args=['Test-Organization']), follow=True) self.assertEqual(response.status_code, 200)
def test_organization_form(self): admin = AdminClient() admin.login_as_admin() # Get an empty form response = admin.get(reverse('organization_create')) self.assertEqual(response.status_code, 200) soup = BeautifulSoup(response.content) # Should have 28 fields total self.assertEqual(len(soup.find_all('input')), 28)
def test_user_access_denied(self): """Regular users should not get access Only admin can use Organization features """ admin_client = AdminClient() admin_client.login_as_non_admin() response = admin_client.get(reverse('organization_create'), follow=True) # Test HTTP denied self.assertEqual(response.status_code, 403)
def test_detail_page_forms(self): c = AdminClient() c.login_as_admin() layer = Layer.objects.first() response = c.get(reverse('layer_detail', args=[layer.typename])) self.assertEqual(response.status_code, 200) # keywords_form test old_keywords = layer.keywords.all() old_keywords_names = [] for okeyword in old_keywords: old_keywords_names.append(okeyword.name) form_data = {'keywords': 'test, test2'} response = c.post(reverse('layer_detail', args=[layer.typename]), data=form_data) self.assertEqual(response.status_code, 200) # Make sure the layer's keywords are updated layer = Layer.objects.filter(id=layer.id)[0] new_keywords = layer.keywords.all() new_keywords_names = [] for nkeyword in new_keywords: new_keywords_names.append(nkeyword.name) self.assertFalse(old_keywords_names == new_keywords_names) # metadata_form test old_metadata = {'title': layer.title, 'category': layer.category, 'language': layer.language, 'distribution_url': layer.distribution_url, 'data_quality_statement': layer.data_quality_statement, 'purpose': layer.purpose, 'is_published': layer.is_published} # distribution url doesn't seem to be modifiable form_data = {'title': 'New title', 'category': '1', 'language': 'fra', 'distribution_url': layer.distribution_url, 'data_quality_statement': 'This is quality', 'purpose': 'To educate', 'is_published': 'on'} # The submitted data as it will appear in the model is slightly different submitted_data = {'title': unicode('New title'), 'category': TopicCategory.objects.first(), 'language': unicode('fra'), 'distribution_url': unicode(layer.distribution_url), 'data_quality_statement': unicode('This is quality'), 'purpose': unicode('To educate'), 'is_published': True} response = c.post(reverse('layer_detail', args=[layer.typename]), data=form_data) self.assertEqual(response.status_code, 200) layer = Layer.objects.filter(id=layer.id)[0] new_metadata = {'title': layer.title, 'category': layer.category, 'language': layer.language, 'distribution_url': layer.distribution_url, 'data_quality_statement': layer.data_quality_statement, 'purpose': layer.purpose, 'is_published': layer.is_published} self.assertFalse(new_metadata == old_metadata) self.assertEqual(submitted_data, new_metadata) # Make sure the keywords have been retained layer_keywords_names = [] for lkeyword in layer.keywords.all(): layer_keywords_names.append(lkeyword.name) self.assertEqual(layer_keywords_names, new_keywords_names)
def test_guest_access_redirect(self): """Guests should not be allowed. Guests should be redirected when not given access. """ admin_client = AdminClient() response = admin_client.get(reverse('organization_create'), follow=True) # Test correct redirect to login page last_url, status_code = response.redirect_chain[-1] self.assertRedirects(response, '/account/login/?next=/organizations/create/', status_code=302, target_status_code=200) self.assertEqual( last_url, 'http://testserver/account/login/?next=/organizations/create/')
def test_social_page_renders(self): admin_client = AdminClient() admin_client.login_as_admin() response = admin_client.get(reverse('profile_detail', args=[self.username])) # Test for final status code = HTTP OK self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'people/profile_detail.html') # Should have all the tab names shown self.assertContains(response, 'Stories') self.assertContains(response, 'Layers') self.assertContains(response, 'Uploads') self.assertContains(response, 'Messages') self.assertContains(response, 'Activity Feed') self.assertContains(response, 'Journal Entries') self.assertContains(response, 'Favorites') self.assertContains(response, 'Icons') self.assertContains(response, 'Stories')
def test_social_page_renders(self): admin_client = AdminClient() admin_client.login_as_admin() response = admin_client.get( reverse('profile_detail', args=[self.username])) # Test for final status code = HTTP OK self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'people/profile_detail.html') # Should have all the tab names shown self.assertContains(response, 'Stories') self.assertContains(response, 'Layers') self.assertContains(response, 'Uploads') self.assertContains(response, 'Messages') self.assertContains(response, 'Activity Feed') self.assertContains(response, 'Journal Entries') self.assertContains(response, 'Favorites') self.assertContains(response, 'Icons') self.assertContains(response, 'Stories')
def test_organization_member_remove(self): """Organization member remove url(r'^organizations/(?P<slug>[^/]*)/member_remove/(?P<username>.+)$', organization_member_remove, name='organization_member_remove'), """ admin = AdminClient() admin.login_as_admin() # Should reject GET requests response = admin.get(reverse('organization_member_remove', args=['Test-Organization', 'admin']), follow=True) self.assertEqual(response.status_code, 200) # TODO: Put good data in here form_data = {} # Should accept POST requests response = admin.post(reverse('organization_member_remove', args=['Test-Organization', 'admin']), follow=True, data=form_data) self.assertEqual(response.status_code, 403)
class ProfileDetailViewTest(MapStoryTestMixin): def setUp(self): self.test_username, self.test_password = self.create_user( 'testingProfiles', 'testingProfiles') self.userclient = AdminClient() def tearDown(self): pass def test_profile_detail_not_found(self): # Should build detail URL correctly self.assertEqual( reverse('profile_detail', kwargs={'slug': 'nonexistent'}), u'/storyteller/nonexistent/') # Should not find this user response = self.client.get( reverse('profile_detail', kwargs={'slug': 'nonexistent'})) self.assertEqual(response.status_code, 404) def test_page_detail_page_response(self): # We need an existing user for this testUser = getTestUser() response = self.client.get(testUser.get_absolute_url()) # The profile page should exist self.assertEqual(response.status_code, 200) # Should be using the correct template self.assertTemplateUsed(response, 'people/profile_detail.html') self.assertContains(response, testUser.first_name) def test_get_username_none(self): response = self.client.get(reverse('edit_profile', kwargs={'username': None}), follow=True) self.assertEqual(response.status_code, 200) def test_profile_edit_page_responses(self): otherUser = getTestUser() other_url = reverse('edit_profile', kwargs={'username': otherUser.username}) self.assertEqual(other_url, u'/storyteller/edit/%s/' % otherUser.username) # Anonymous users should be redirected to login form response = self.client.get(other_url, follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'account/login.html') self.assertContains(response, 'Log in to an existing account') # Login with a user edit_user_url = reverse('edit_profile', kwargs={'username': self.test_username}) self.userclient.login_as_non_admin(username=self.test_username, password=self.test_password) response = self.userclient.get(edit_user_url) self.assertEqual(response.status_code, 200) self.assertContains(response, self.test_username) self.assertTemplateUsed(response, 'people/profile_edit.html') self.assertContains(response, 'Edit Your Profile') # Create new organization form_data = { 'first_name': 'editedtestname', 'last_name': 'editedtestname', } response = self.userclient.post(edit_user_url, data=form_data, follow=True) # Should not let other users edit profiles they don't own response = self.userclient.get(other_url) self.assertEqual(response.status_code, 403) def test_profile_delete_anonymous_user_delete(self): # Should redirect to the login page response = self.client.get(reverse( 'profile_delete', kwargs={'username': '******'}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'account/login.html') def test_profile_delete_not_found(self): self.userclient.login_as_non_admin(username=self.test_username, password=self.test_password) response = self.userclient.get(reverse( 'profile_delete', kwargs={'username': '******'}), follow=True) self.assertEqual(response.status_code, 404) def test_profile_delete_get(self): self.userclient.login_as_non_admin(username=self.test_username, password=self.test_password) response = self.userclient.get(reverse( 'profile_delete', kwargs={'username': self.test_username}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'people/profile_delete.html') def test_profile_delete_post(self): self.userclient.login_as_non_admin(username=self.test_username, password=self.test_password) # Create new organization form_data = { 'is_active': False, } response = self.userclient.post(reverse( 'profile_delete', kwargs={'username': self.test_username}), data=form_data, follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'index.html') response = self.client.get(reverse('profile_detail', kwargs={'slug': self.test_username}), follow=True) self.assertEqual(response.status_code, 200)
class LayerTests(MapStoryTestMixin): def setUp(self): self.admin_client = AdminClient() def login_admin(self): """ Convenience method for loging in as a superuser. """ try: user = User.objects.get(username='******') self.assertIsNotNone(user) except User.DoesNotExist: create_admin_user("admin", "admin") self.admin_client.login_as_admin("admin", "admin") @skip("Fix this") def test_create_layer(self): filename = generate_testname(prefix='layer_', size=5) payload = { "featureType": { "attributes": { "attribute": [ {"name": "geometry", "binding": "com.vividsolutions.jts.geom.Point", "minOccurs": 0, "nillable": True}, {"name": "time", "binding": "org.geotools.data.postgis.BigDate", "nillable": True, "minOccurs": 0} ] }, "nativeCRS": "EPSG:4326", "srs": "EPSG:4326", "store": {"name": "mapstory_geogig"}, "namespace": {"name": "geonode"}, "configureTime": True, "editable": True, "name": filename, "start_date": "time", "permissions": { "users": { "AnonymousUser": ["change_layer_data", "download_resourcebase", "view_resourcebase"] }, "groups": { "registered": ["change_layer_data", "download_resourcebase", "view_resourcebase"] } }, "storeCreateGeogig": True } } self.login_admin() response = self.admin_client.post( reverse('layer_create'), data=json.dumps(payload), content_type='application/json', HTTP_X_REQUESTED_WITH='XMLHttpRequest', ) self.assertEqual(201, response.status_code) json_response = json.loads(response.content) self.assertTrue(u'layers' in json_response.keys()) self.assertTrue(len(json_response[u'layers']) > 0) def test_layer_upload_needs_login(self): # Should not be authorized to upload things with open(test_layer_file_path) as fp: response = self.client.post('/uploads/new/json', {'name': 'file', 'attachment': fp}, follow=True) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'account/login.html') def test_csv_layer_upload(self): self.login_admin() with open(test_layer_file_path) as fp: response = self.admin_client.post( '/uploads/new/json', {'name': 'file', 'attachment': fp}, follow=True ) self.assertEqual(response.status_code, 200) self.assertTemplateNotUsed(response, 'account/login.html') def test_layer_import_wizard_views(self): # Should have 0 Layers stored response = self.admin_client.get('/maps/new', follow=True) self.assertEquals(response.status_code, 200) self.assertTemplateNotUsed(response, 'account/login.html') self.assertEquals(0, len(Layer.objects.all())) self.login_admin() with open(test_layer_file_path) as fp: # --------------- # 1. Upload file # --------------- response = self.admin_client.post('/uploads/new/json', {'name': 'testlayer', 'file': fp}, follow=True) self.assertEquals(response.status_code, 200) # Should Respond with JSON response_json = json.loads(response.content) self.assertIsNotNone(response_json) # Should not have any errors self.assertFalse(u'errors' in response_json.keys()) # State should be 'UPLOADED; self.assertTrue(u'state' in response_json.keys()) self.assertEquals(u'UPLOADED', response_json[u'state']) # Should have an upload ID self.assertIsNotNone(response_json[u'id']) # -------------------------- # 2. Get Upload detail info # -------------------------- url = "/importer-api/data/%s" % (response_json[u'id'],) detail_response = self.admin_client.get(url, follow=True) self.assertEquals(200, detail_response.status_code) # Should respond with JSON detail_json = json.loads(detail_response.content) self.assertIsNotNone(detail_json) # Should have upload detail information self.assertTrue(u'name' in detail_json.keys()) self.assertEquals(True, detail_json[u'complete']) self.assertIsNotNone(detail_json[u'date']) self.assertEquals('CSV', detail_json[u'file_type']) self.assertIsNotNone(detail_json[u'resource_uri']) self.assertIsNotNone(detail_json[u'name']) # Get the uploaded details upload_name = detail_json[u'name'] # ---------------------- # 3. POST configuration # ---------------------- json_str = json.dumps({ "always_geogig": True, "configureTime": True, "editable": True, "convert_to_date": ["e_date", "fdate"], "index": 0, "name": upload_name, "permissions": { "users": { "AnonymousUser": [ "change_layer_data", "download_resourcebase", "view_resourcebase"] }, "groups": { "registered": [ "change_layer_data", "download_resourcebase", "view_resourcebase" ] } }, "start_date": "e_date", "end_date": "fdate", "geoserver_store": { "type": "geogig" } }) url = "/importer-api/data-layers/%s/configure/" % (response_json[u'id'],) config_response = self.admin_client.post(url, json_str, content_type="application/json") self.assertEquals(config_response.status_code, 200) # Should receive a JSON task json_task = json.loads(config_response.content) self.assertIsNotNone(json_task) self.assertTrue(u'task' in json_task.keys()) taskid = json_task[u'task'] self.assertIsNotNone(taskid) # ------------------- # 4. Get data layers url = "/importer-api/data-layers/%s/" % (response_json[u'id'],) response = self.admin_client.get(url, follow=True) self.assertEquals(response.status_code, 200) self.assertTemplateNotUsed(response, 'account/login.html') # Should get JSON back json_response = json.loads(response.content) self.assertIsNotNone(json_response) retries = 0 while retries < 5: time.sleep(2) # Try again retry_response = self.admin_client.get(url, follow=True) self.assertEquals(retry_response.status_code, 200) json_response = json.loads(retry_response.content) self.assertIsNotNone(json_response) if json_response[u'status'] == u'SUCCESS': break else: retries += 1
class ProfileDetailViewTest(MapStoryTestMixin): def setUp(self): self.test_username, self.test_password = self.create_user( 'testingProfiles', 'testingProfiles') self.userclient = AdminClient() def tearDown(self): pass def test_profile_detail_not_found(self): # Should build detail URL correctly self.assertEqual(reverse('profile_detail', kwargs={ 'slug': 'nonexistent'}), u'/storyteller/nonexistent/') # Should not find this user response = self.client.get( reverse('profile_detail', kwargs={'slug': 'nonexistent'})) self.assertEqual(response.status_code, 404) def test_page_detail_page_response(self): # We need an existing user for this testUser = getTestUser() response = self.client.get(testUser.get_absolute_url()) # The profile page should exist self.assertEqual(response.status_code, 200) # Should be using the correct template self.assertTemplateUsed(response, 'people/profile_detail.html') self.assertContains(response, testUser.first_name) def test_get_username_none(self): response = self.client.get(reverse('profile_edit', kwargs={ 'username': None}), follow=True) self.assertEqual(response.status_code, 200) def test_profile_edit_page_responses(self): otherUser = getTestUser() other_url = reverse('profile_edit', kwargs={ 'username': otherUser.username}) self.assertEqual(other_url, u'/storyteller/edit/%s/' % otherUser.username) # Anonymous users should be redirected to login form response = self.client.get(other_url, follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'account/login.html') self.assertContains(response, 'Log in to an existing account') # Login with a user edit_user_url = reverse('profile_edit', kwargs={ 'username': self.test_username}) self.userclient.login_as_non_admin( username=self.test_username, password=self.test_password) response = self.userclient.get(edit_user_url) self.assertEqual(response.status_code, 200) self.assertContains(response, self.test_username) self.assertTemplateUsed(response, 'people/profile_edit.html') self.assertContains(response, 'Edit Your Profile') # Create new organization form_data = { 'first_name': 'editedtestname', 'last_name': 'editedtestname', } response = self.userclient.post( edit_user_url, data=form_data, follow=True) # Should not let other users edit profiles they don't own response = self.userclient.get(other_url) self.assertEqual(response.status_code, 403) def test_users_cannot_edit_other_users(self): factory = RequestFactory() request = factory.get('storyteller/edit/admin') testUser = getTestUser() request.user = testUser request.session = {} response = profile_edit(request, None) # Server should refuse! self.assertEqual(response.status_code, 403) def test_profile_delete_anonymous_user_delete(self): # Should redirect to the login page response = self.client.get(reverse('profile_delete', kwargs={ 'username': '******'}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'account/login.html') def test_profile_delete_not_found(self): self.userclient.login_as_non_admin( username=self.test_username, password=self.test_password) response = self.userclient.get(reverse('profile_delete', kwargs={ 'username': '******'}), follow=True) self.assertEqual(response.status_code, 404) def test_profile_delete_get(self): self.userclient.login_as_non_admin( username=self.test_username, password=self.test_password) response = self.userclient.get(reverse('profile_delete', kwargs={ 'username': self.test_username}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'people/profile_delete.html')
class ProfileDetailViewTest(MapStoryTestMixin): def setUp(self): self.test_username, self.test_password = self.create_user( 'testingProfiles', 'testingProfiles') self.userclient = AdminClient() def tearDown(self): pass def test_profile_detail_not_found(self): # Should build detail URL correctly self.assertEqual( reverse('profile_detail', kwargs={'slug': 'nonexistent'}), u'/storyteller/nonexistent/') # Should not find this user response = self.client.get( reverse('profile_detail', kwargs={'slug': 'nonexistent'})) self.assertEqual(response.status_code, 404) def test_page_detail_page_response(self): # We need an existing user for this testUser = getTestUser() response = self.client.get(testUser.get_absolute_url()) # The profile page should exist self.assertEqual(response.status_code, 200) # Should be using the correct template self.assertTemplateUsed(response, 'people/profile_detail.html') self.assertContains(response, testUser.first_name) def test_get_username_none(self): response = self.client.get(reverse('profile_edit', kwargs={'username': None}), follow=True) self.assertEqual(response.status_code, 200) def test_profile_edit_page_responses(self): otherUser = getTestUser() other_url = reverse('profile_edit', kwargs={'username': otherUser.username}) self.assertEqual(other_url, u'/storyteller/edit/%s/' % otherUser.username) # Anonymous users should be redirected to login form response = self.client.get(other_url, follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'account/login.html') self.assertContains(response, 'Log in to an existing account') # Login with a user edit_user_url = reverse('profile_edit', kwargs={'username': self.test_username}) self.userclient.login_as_non_admin(username=self.test_username, password=self.test_password) response = self.userclient.get(edit_user_url) self.assertEqual(response.status_code, 200) self.assertContains(response, self.test_username) self.assertTemplateUsed(response, 'people/profile_edit.html') self.assertContains(response, 'Edit Your Profile') # Create new organization form_data = { 'first_name': 'editedtestname', 'last_name': 'editedtestname', } response = self.userclient.post(edit_user_url, data=form_data, follow=True) # Should not let other users edit profiles they don't own response = self.userclient.get(other_url) self.assertEqual(response.status_code, 403) def test_profile_edit_no_profile_exception(self): factory = RequestFactory() created = User.objects.create_user( username='******', email='*****@*****.**', password='******') self.assertIsNotNone(created) # Raise the No Profile exception when getting the profile request = factory.get( reverse('profile_edit', kwargs={'username': None})) created.profile = PropertyMock(return_value=Profile.DoesNotExist()) request.user = created response = profile_edit(request, None) # TODO(Zunware): Discover why we are getting a forbidden http error # self.assertEqual(response.status_code, 200) def test_profile_edit_with_username_none(self): factory = RequestFactory() # Create an un-authed request created = User.objects.create_user( username='******', email='*****@*****.**', password='******') self.assertIsNotNone(created) request = factory.get( reverse('profile_edit', kwargs={'username': None})) request.user = created # Get a response response = profile_edit(request, None) # TODO(Zunware): Discover why we are getting a forbidden http error # self.assertEqual(response.status_code, 200) def test_users_cannot_edit_other_users(self): factory = RequestFactory() request = factory.get('storyteller/edit/admin') testUser = getTestUser() request.user = testUser request.session = {} response = profile_edit(request, None) # Server should refuse! self.assertEqual(response.status_code, 403) def test_profile_delete_anonymous_user_delete(self): # Should redirect to the login page response = self.client.get(reverse( 'profile_delete', kwargs={'username': '******'}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'account/login.html') def test_profile_delete_not_found(self): self.userclient.login_as_non_admin(username=self.test_username, password=self.test_password) response = self.userclient.get(reverse( 'profile_delete', kwargs={'username': '******'}), follow=True) self.assertEqual(response.status_code, 404) def test_profile_delete_get(self): self.userclient.login_as_non_admin(username=self.test_username, password=self.test_password) response = self.userclient.get(reverse( 'profile_delete', kwargs={'username': self.test_username}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'people/profile_delete.html') def test_profile_delete_post(self): self.userclient.login_as_non_admin(username=self.test_username, password=self.test_password) # Create new organization form_data = { 'is_active': False, } response = self.userclient.post(reverse( 'profile_delete', kwargs={'username': self.test_username}), data=form_data, follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'index.html') response = self.client.get(reverse('profile_detail', kwargs={'slug': self.test_username}), follow=True) self.assertEqual(response.status_code, 200)
class LayerTests(MapStoryTestMixin): def setUp(self): self.admin_client = AdminClient() def login_admin(self): """ Convenience method for loging in as a superuser. """ try: user = User.objects.get(username='******') self.assertIsNotNone(user) except User.DoesNotExist: create_admin_user("admin", "admin") self.admin_client.login_as_admin("admin", "admin") @skip("Fix this") def test_create_layer(self): filename = generate_testname(prefix='layer_', size=5) payload = { "featureType": { "attributes": { "attribute": [{ "name": "geometry", "binding": "com.vividsolutions.jts.geom.Point", "minOccurs": 0, "nillable": True }, { "name": "time", "binding": "org.geotools.data.postgis.BigDate", "nillable": True, "minOccurs": 0 }] }, "nativeCRS": "EPSG:4326", "srs": "EPSG:4326", "store": { "name": "mapstory_geogig" }, "namespace": { "name": "geonode" }, "configureTime": True, "editable": True, "name": filename, "start_date": "time", "permissions": { "users": { "AnonymousUser": [ "change_layer_data", "download_resourcebase", "view_resourcebase" ] }, "groups": { "registered": [ "change_layer_data", "download_resourcebase", "view_resourcebase" ] } }, "storeCreateGeogig": True } } self.login_admin() response = self.admin_client.post( reverse('layer_create'), data=json.dumps(payload), content_type='application/json', HTTP_X_REQUESTED_WITH='XMLHttpRequest', ) self.assertEqual(201, response.status_code) json_response = json.loads(response.content) self.assertTrue(u'layers' in json_response.keys()) self.assertTrue(len(json_response[u'layers']) > 0) def test_layer_upload_needs_login(self): # Should not be authorized to upload things with open(test_layer_file_path) as fp: response = self.client.post('/uploads/new/json', { 'name': 'file', 'attachment': fp }, follow=True) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'account/login.html') def test_csv_layer_upload(self): self.login_admin() with open(test_layer_file_path) as fp: response = self.admin_client.post('/uploads/new/json', { 'name': 'file', 'attachment': fp }, follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateNotUsed(response, 'account/login.html') def test_layer_import_wizard_views(self): # Should have 0 Layers stored response = self.admin_client.get('/maps/new', follow=True) self.assertEquals(response.status_code, 200) self.assertTemplateNotUsed(response, 'account/login.html') self.assertEquals(0, len(Layer.objects.all())) self.login_admin() with open(test_layer_file_path) as fp: # --------------- # 1. Upload file # --------------- response = self.admin_client.post('/uploads/new/json', { 'name': 'testlayer', 'file': fp }, follow=True) self.assertEquals(response.status_code, 200) # Should Respond with JSON response_json = json.loads(response.content) self.assertIsNotNone(response_json) # Should not have any errors self.assertFalse(u'errors' in response_json.keys()) # State should be 'UPLOADED; self.assertTrue(u'state' in response_json.keys()) self.assertEquals(u'UPLOADED', response_json[u'state']) # Should have an upload ID self.assertIsNotNone(response_json[u'id']) # -------------------------- # 2. Get Upload detail info # -------------------------- url = "/importer-api/data/%s" % (response_json[u'id'], ) detail_response = self.admin_client.get(url, follow=True) self.assertEquals(200, detail_response.status_code) # Should respond with JSON detail_json = json.loads(detail_response.content) self.assertIsNotNone(detail_json) # Should have upload detail information self.assertTrue(u'name' in detail_json.keys()) self.assertEquals(True, detail_json[u'complete']) self.assertIsNotNone(detail_json[u'date']) self.assertEquals('CSV', detail_json[u'file_type']) self.assertIsNotNone(detail_json[u'resource_uri']) self.assertIsNotNone(detail_json[u'name']) # Get the uploaded details upload_name = detail_json[u'name'] # ---------------------- # 3. POST configuration # ---------------------- json_str = json.dumps({ "always_geogig": True, "configureTime": True, "editable": True, "convert_to_date": ["e_date", "fdate"], "index": 0, "name": upload_name, "permissions": { "users": { "AnonymousUser": [ "change_layer_data", "download_resourcebase", "view_resourcebase" ] }, "groups": { "registered": [ "change_layer_data", "download_resourcebase", "view_resourcebase" ] } }, "start_date": "e_date", "end_date": "fdate", "geoserver_store": { "type": "geogig" } }) url = "/importer-api/data-layers/%s/configure/" % ( response_json[u'id'], ) config_response = self.admin_client.post( url, json_str, content_type="application/json") self.assertEquals(config_response.status_code, 200) # Should receive a JSON task json_task = json.loads(config_response.content) self.assertIsNotNone(json_task) self.assertTrue(u'task' in json_task.keys()) taskid = json_task[u'task'] self.assertIsNotNone(taskid) # ------------------- # 4. Get data layers url = "/importer-api/data-layers/%s/" % (response_json[u'id'], ) response = self.admin_client.get(url, follow=True) self.assertEquals(response.status_code, 200) self.assertTemplateNotUsed(response, 'account/login.html') # Should get JSON back json_response = json.loads(response.content) self.assertIsNotNone(json_response) retries = 0 while retries < 5: time.sleep(2) # Try again retry_response = self.admin_client.get(url, follow=True) self.assertEquals(retry_response.status_code, 200) json_response = json.loads(retry_response.content) self.assertIsNotNone(json_response) if json_response[u'status'] == u'SUCCESS': break else: retries += 1
def test_detail_page_forms(self): c = AdminClient() c.login_as_admin() layer = Layer.objects.first() response = c.get(reverse('layer_detail', args=[layer.typename])) self.assertEqual(response.status_code, 200) # keywords_form test old_keywords = layer.keywords.all() old_keywords_names = [] for okeyword in old_keywords: old_keywords_names.append(okeyword.name) form_data = {'keywords': 'test, test2'} response = c.post(reverse('layer_detail', args=[layer.typename]), data=form_data) self.assertEqual(response.status_code, 200) # Make sure the layer's keywords are updated layer = Layer.objects.filter(id=layer.id)[0] new_keywords = layer.keywords.all() new_keywords_names = [] for nkeyword in new_keywords: new_keywords_names.append(nkeyword.name) self.assertFalse(old_keywords_names == new_keywords_names) # metadata_form test old_metadata = { 'title': layer.title, 'category': layer.category, 'language': layer.language, 'distribution_url': layer.distribution_url, 'data_quality_statement': layer.data_quality_statement, 'purpose': layer.purpose, 'is_published': layer.is_published } # distribution url doesn't seem to be modifiable form_data = { 'title': 'New title', 'category': '1', 'language': 'fra', 'distribution_url': layer.distribution_url, 'data_quality_statement': 'This is quality', 'purpose': 'To educate', 'is_published': 'on' } # The submitted data as it will appear in the model is slightly different submitted_data = { 'title': unicode('New title'), 'category': TopicCategory.objects.first(), 'language': unicode('fra'), 'distribution_url': unicode(layer.distribution_url), 'data_quality_statement': unicode('This is quality'), 'purpose': unicode('To educate'), 'is_published': True } response = c.post(reverse('layer_detail', args=[layer.typename]), data=form_data) self.assertEqual(response.status_code, 200) layer = Layer.objects.filter(id=layer.id)[0] new_metadata = { 'title': layer.title, 'category': layer.category, 'language': layer.language, 'distribution_url': layer.distribution_url, 'data_quality_statement': layer.data_quality_statement, 'purpose': layer.purpose, 'is_published': layer.is_published } self.assertFalse(new_metadata == old_metadata) self.assertEqual(submitted_data, new_metadata) # Make sure the keywords have been retained layer_keywords_names = [] for lkeyword in layer.keywords.all(): layer_keywords_names.append(lkeyword.name) self.assertEqual(layer_keywords_names, new_keywords_names)
class MapStoryOrganizationTests(MapStoryTestMixin): def setUp(self): self.username, self.password = self.create_user('admin', 'admin', is_superuser=True) self.non_admin_username, self.non_admin_password = self.create_user( 'non_admin', 'non_admin') self.admin_client = AdminClient() self.admin_client.login_as_admin() self.form_data = { 'social_twitter': 'notreal', 'social_facebook': 'notreal', 'title': 'Test Organization', 'description': 'Testing', 'email': '*****@*****.**', 'access': 'public', 'date_joined': datetime.now(), 'city': 'Victoria', 'country': 'CAN', 'keywords': 'test', 'profile_type': 'org', 'slug': 'Test-Organization' } response = self.admin_client.post(reverse('organization_create'), data=self.form_data, follow=True) # Test for final status code = HTTP OK self.assertEqual(response.status_code, 200) # Test for correct redirect last_url, status_code = response.redirect_chain[-1] self.assertRedirects(response, '/organizations/' + self.form_data['slug'], status_code=302, target_status_code=200) # When the organization is created, a GroupProfile and Collection model pointing to it should be created group = GroupProfile.objects.all().first() collection = Collection.objects.all().first() self.assertEqual(collection.group, group) def test_organization_create(self): # When the organization is created, a GroupProfile and Collection model pointing to it should be created group = GroupProfile.objects.all().first() collection = Collection.objects.all().first() self.assertEqual(collection.group, group) manager = group.get_managers().all() # Should only have 1 manager self.assertEqual(len(manager), 1) self.assertEqual(group.profile_type, 'org') def test_organization_create_get(self): """Should return an empty form. """ response = self.admin_client.get(reverse('organization_create'), follow=True) self.assertEqual(response.status_code, 200) def test_organization_create_post(self): """Should create a new organization """ admin_client = AdminClient() admin_client.login_as_admin() # Create new organization form_data = { 'social_twitter': 'notreal', 'social_facebook': 'notreal', 'title': 'Test Organization Two', 'description': 'Testing', 'email': '*****@*****.**', 'access': 'public', 'date_joined': datetime.now(), 'city': 'Cholula', 'country': 'MEX', 'keywords': 'test', 'profile_type': 'org', 'slug': 'Test-Organization-Two' } response = admin_client.post(reverse('organization_create'), data=form_data, follow=True) self.assertEqual(response.status_code, 200) # When the organization is created, a GroupProfile and Collection model pointing to it should be created group = GroupProfile.objects.all().first() collection = Collection.objects.all().first() self.assertEqual(collection.group, group) self.assertEqual(collection.name, group.title) self.assertEqual(collection.slug, group.slug) self.assertEqual(group.profile_type, 'org')