Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test setting public flag via the REST API. #100

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Test setting public flag via the REST API.
  • Loading branch information
movestill committed Nov 16, 2021
commit c8c67f9d075b05b26d26fe5c43b88f17bdf77c74
7 changes: 7 additions & 0 deletions django/bosscore/test/setup_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,19 @@
EXP22 = 'exp22'
EXP_BASE_RES = 'exp-base-res-test'
TEST_DATA_EXPERIMENTS = [EXP1, EXP22, EXP_BASE_RES]
EXP_NOT_PUBLIC = 'exp-not-public'

CHAN_BASE_RES = 'chan-with-base-res'
CHAN_NOT_PUBLIC = 'chan-not-public'

# Channel for cloudvolume tests uses this bucket name.
CLOUD_VOL_BUCKET = 'bossdb-test-data'
CVPATH_CHAN1 = 'col1/exp1/chan1'
CVPATH_CHAN2 = 'col1/exp1/chan2'
CVPATH_ANNO1 = 'col1/exp1/anno1'

# Collection names.
COLL_NOT_PUBLIC = 'col-not-public'

class SetupTestDB:
def __init__(self, super_user=None):
Expand Down Expand Up @@ -134,18 +138,21 @@ def insert_test_data(self):
self.add_collection('col1', 'Description for collection1')
self.add_collection('col1-22', 'Description for collection1-22')
self.add_collection('col2', 'Description for collection2')
self.add_collection(COLL_NOT_PUBLIC, 'Collection to test setting public', public=False)

self.add_coordinate_frame('cf1', 'Description for cf1', 0, 1000, 0, 1000, 0, 1000, 4, 4, 4)

self.add_experiment('col1', EXP1, 'cf1', NUM_HIERARCHY_LEVELS, 10, 1)
self.add_experiment('col1', EXP22, 'cf1', NUM_HIERARCHY_LEVELS, 500, 1)
self.add_experiment('col1', EXP_BASE_RES, 'cf1', NUM_HIERARCHY_LEVELS, 10, 1)
self.add_experiment(COLL_NOT_PUBLIC, EXP_NOT_PUBLIC, 'cf1', NUM_HIERARCHY_LEVELS, 1, 1, public=False)

self.add_channel('col1', EXP1, 'channel1', 0, 0, 'uint8', 'image')
self.add_channel('col1', EXP1, 'channel2', 0, 0, 'uint8', 'image')
self.add_channel('col1', EXP1, 'channel3', 0, 0, 'uint64', 'annotation', ['channel1'])
self.add_channel('col1', EXP_BASE_RES, CHAN_BASE_RES, 0, BASE_RESOLUTION, 'uint8', 'image')
self.add_channel('col1', EXP1, 'layer1', 0, 0, 'uint64', 'annotation', ['channel1'])
self.add_channel(COLL_NOT_PUBLIC, EXP_NOT_PUBLIC, CHAN_NOT_PUBLIC, 0, 0, 'uint8', 'image', public=False)

def insert_lookup_test_data(self):
"""
Expand Down
176 changes: 154 additions & 22 deletions django/bosscore/test/test_resource_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

from rest_framework.test import APITestCase
from django.conf import settings
from .setup_db import SetupTestDB, TEST_DATA_EXPERIMENTS
from .setup_db import (
SetupTestDB, TEST_DATA_EXPERIMENTS,
COLL_NOT_PUBLIC, EXP_NOT_PUBLIC, CHAN_NOT_PUBLIC,
)
from bosscore.models import Channel

version = settings.BOSS_VERSION
Expand All @@ -29,13 +32,13 @@ def setUp(self):
Initialize the database
:return:
"""
dbsetup = SetupTestDB()
user = dbsetup.create_user('testuser')
dbsetup.add_role('resource-manager')
dbsetup.set_user(user)
self.dbsetup = SetupTestDB()
user = self.dbsetup.create_user('testuser')
self.dbsetup.add_role('resource-manager')
self.dbsetup.set_user(user)

self.client.force_login(user)
dbsetup.insert_test_data()
self.dbsetup.insert_test_data()

def test_get_collection_doesnotexist(self):
"""
Expand All @@ -59,6 +62,7 @@ def test_get_collection_exist(self):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['name'], 'col1')
self.assertFalse(response.data['public'])

def test_post_collection(self):
"""
Expand All @@ -71,6 +75,19 @@ def test_post_collection(self):
# Get an existing collection
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 201)
self.assertFalse(response.data['public'])

def test_post_collection_public(self):
url = '/' + version + '/collection/col55'
data = {
'description': 'A new public collection for unit tests',
'public': True,
}

# Get an existing collection
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 201)
self.assertTrue(response.data['public'])

def test_post_collection_special_characters(self):
"""
Expand Down Expand Up @@ -122,6 +139,34 @@ def test_put_collection_exists(self):
response = self.client.put(url, data=data)
self.assertEqual(response.status_code, 200)

def test_put_collection_make_public(self):
"""
Test making collection public.

"""
url = f'/{version}/collection/{COLL_NOT_PUBLIC}/'
data = {'public': True}

response = self.client.put(url, data=data)
self.assertEqual(response.status_code, 200)
self.assertTrue(response.data['public'])

def test_put_collection_no_permission(self):
"""
Test updating a collection w/o ResourceManager role forbidden.

"""
url = f'/{version}/collection/{COLL_NOT_PUBLIC}/'
data = {'public': True}

user = self.dbsetup.create_user('nonresourcemgr')
self.dbsetup.set_user(user)

self.client.force_login(user)

response = self.client.put(url, data=data)
self.assertEqual(response.status_code, 403)

def test_put_collection_doesnotexist(self):
"""
Update a collection that does not exist
Expand Down Expand Up @@ -158,7 +203,6 @@ def test_delete_collection(self):
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 201)

# Get an existing collection
response = self.client.delete(url)
self.assertEqual(response.status_code, 204)

Expand Down Expand Up @@ -227,13 +271,13 @@ def setUp(self):
Initialize the database

"""
dbsetup = SetupTestDB()
user = dbsetup.create_user('testuser')
dbsetup.add_role('resource-manager')
dbsetup.set_user(user)
self.dbsetup = SetupTestDB()
user = self.dbsetup.create_user('testuser')
self.dbsetup.add_role('resource-manager')
self.dbsetup.set_user(user)

self.client.force_login(user)
dbsetup.insert_test_data()
self.dbsetup.insert_test_data()

def test_get_experiment_doesnotexist(self):
"""
Expand All @@ -257,6 +301,7 @@ def test_get_experiment_exist(self):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['name'], 'exp1')
self.assertFalse(response.data['public'])

def test_post_experiment(self):
"""
Expand Down Expand Up @@ -394,6 +439,22 @@ def test_post_experiment_no_data(self):
response = self.client.post(url)
self.assertEqual(response.status_code, 400)

def test_post_experiment_public(self):
# Get the coordinate frame id
url = '/' + version + '/coord/cf1'
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
cf = response.data['name']

# Post a new experiment
url = '/' + version + '/collection/col1/experiment/exp2'
data = {'description': 'This is a public new experiment', 'coord_frame': cf,
'num_hierarchy_levels': 10, 'hierarchy_method': 'isotropic', 'num_time_samples': 10,
'public': True}
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 201)
self.assertTrue(response.data['public'])

def test_put_experiment_exists(self):
"""
Update a experiment (Valid - The experiment exists)
Expand Down Expand Up @@ -428,6 +489,34 @@ def test_put_experiment_name(self):
response = self.client.put(url, data=data)
self.assertEqual(response.status_code, 200)

def test_put_experiment_public(self):
"""
Test making experiment public.

"""
url = f'/{version}/collection/{COLL_NOT_PUBLIC}/experiment/{EXP_NOT_PUBLIC}'
data = {'public': True}

response = self.client.put(url, data=data)
self.assertEqual(response.status_code, 200)
self.assertTrue(response.data['public'])

def test_put_experiment_no_permission(self):
"""
Test updating a experiment w/o ResourceManager role forbidden.

"""
url = f'/{version}/collection/{COLL_NOT_PUBLIC}/experiment/{EXP_NOT_PUBLIC}'
data = {'public': True}

user = self.dbsetup.create_user('nonresourcemgr')
self.dbsetup.set_user(user)

self.client.force_login(user)

response = self.client.put(url, data=data)
self.assertEqual(response.status_code, 403)

def test_delete_experiment(self):
"""
Delete a experiment
Expand Down Expand Up @@ -698,14 +787,14 @@ def setUp(self):
Initialize the database

"""
dbsetup = SetupTestDB()
self.super_user = dbsetup.create_super_user()
user = dbsetup.create_user('testuser')
dbsetup.add_role('resource-manager')
dbsetup.set_user(user)
self.dbsetup = SetupTestDB()
self.super_user = self.dbsetup.create_super_user()
user = self.dbsetup.create_user('testuser')
self.dbsetup.add_role('resource-manager')
self.dbsetup.set_user(user)

self.client.force_login(user)
dbsetup.insert_test_data()
self.dbsetup.insert_test_data()

def test_get_channel_doesnotexist(self):
"""
Expand All @@ -730,6 +819,7 @@ def test_get_channel_exist(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['name'], 'channel1')
self.assertEqual(response.data['downsample_status'], 'NOT_DOWNSAMPLED')
self.assertFalse(response.data['public'])

def test_post_channel(self):
"""
Expand All @@ -741,6 +831,7 @@ def test_post_channel(self):
data = {'description': 'This is a new channel', 'datatype': 'uint8', 'type': 'image'}
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 201)
self.assertFalse(response.data['public'])

def test_post_channel_set_cloudvol_storage_no_cv_path(self):
"""
Expand Down Expand Up @@ -858,6 +949,22 @@ def test_post_channel_exists(self):
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 400)

def test_post_channel_public(self):
"""
Test creating a new public channel.

"""
url = '/' + version + '/collection/col1/experiment/exp1/channel/channel33/'
data = {'description': 'This is a new public channel', 'type': 'annotation', 'datatype': 'uint8',
'sources': ['channel1'], 'related': ['channel2'],
'public': True, }
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 201)

response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertTrue(response.data['public'])

def test_post_channel_annotation_without_source(self):
"""
Post a new channel of type annotation w/o providing a source channel.
Expand All @@ -881,7 +988,6 @@ def test_post_channel_annotation_with_source(self):
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 201)

# Get an existing experiment
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['sources'], ['channel1'])
Expand All @@ -899,12 +1005,11 @@ def test_post_channel_annotation_with_multiple_sources(self):
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 201)

# Get an existing experiment
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['sources'], ['channel1', 'channel2'])

# Ensure that this is Asymmetrical
# Ensure that this source relation is asymmetric
url = '/' + version + '/collection/col1/experiment/exp1/channel/channel1/'
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
Expand Down Expand Up @@ -962,7 +1067,6 @@ def test_post_channel_annotation_with_multiple_related(self):
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 201)

# Get an existing experiment
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['related'], ['channel2', 'channel3'])
Expand All @@ -984,6 +1088,18 @@ def test_put_channel(self):
response = self.client.put(url, data=data)
self.assertEqual(response.status_code, 200)

def test_put_channel_public(self):
"""
Test making a channel public.

"""
url = f'/{version}/collection/{COLL_NOT_PUBLIC}/experiment/{EXP_NOT_PUBLIC}/channel/{CHAN_NOT_PUBLIC}'
data = {'public': True}

response = self.client.put(url, data=data)
self.assertEqual(response.status_code, 200)
self.assertTrue(response.data['public'])

def test_put_channel_set_cv_path_forbidden_for_non_admins(self):
"""
Update a channel (Invalid - only admins can set cv_path)
Expand Down Expand Up @@ -1188,6 +1304,22 @@ def test_put_channel_name(self):
response = self.client.put(url, data=data)
self.assertEqual(response.status_code, 200)

def test_put_channel_no_permission(self):
"""
Test updating a channel w/o ResourceManager role forbidden.

"""
url = f'/{version}/collection/{COLL_NOT_PUBLIC}/experiment/{EXP_NOT_PUBLIC}/channel/{CHAN_NOT_PUBLIC}'
data = {'public': True}

user = self.dbsetup.create_user('nonresourcemgr')
self.dbsetup.set_user(user)

self.client.force_login(user)

response = self.client.put(url, data=data)
self.assertEqual(response.status_code, 403)

def test_delete_channel(self):
"""
Delete a channel
Expand Down