Commit 5a7f8d15 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[dataformats][tests] Add module tear down

This commit also cleans up the code.
parent f03ac53a
......@@ -36,72 +36,69 @@ from django.contrib.auth.models import User
from ..models import DataFormat
from ...team.models import Team
from ...common.testutils import BaseTestCase, tearDownModule
from ...common.testutils import BaseTestCase
from ...common.testutils import tearDownModule # noqa test runner will call it
TEST_PWD = "1234"
class DataFormatsAccessibilityFunctionsBase(BaseTestCase):
class DataFormatsAccessibilityFunctionsBase(BaseTestCase):
def setUp(self):
if os.path.exists(settings.DATAFORMATS_ROOT):
shutil.rmtree(settings.DATAFORMATS_ROOT)
# Users
user1 = User.objects.create_user('jackdoe', 'jackdoe@test.org', '1234')
user2 = User.objects.create_user('johndoe', 'johndoe@test.org', '1234')
user3 = User.objects.create_user('janedoe', 'janedoe@test.org', '1234')
User.objects.create_user('jacquelinedoe', 'jacquelinedoe@test.org', '1234')
user1 = User.objects.create_user("jackdoe", "jackdoe@test.org", TEST_PWD)
user2 = User.objects.create_user("johndoe", "johndoe@test.org", TEST_PWD)
user3 = User.objects.create_user("janedoe", "janedoe@test.org", TEST_PWD)
User.objects.create_user("jacquelinedoe", "jacquelinedoe@test.org", TEST_PWD)
team1 = Team.objects.create(name='teamdoe', owner=user1)
team2 = Team.objects.create(name='teamdoe2', owner=user1)
Team.objects.create(name='teamdoe3', owner=user1)
Team.objects.create(name='teamdoe4', owner=user1)
team1 = Team.objects.create(name="teamdoe", owner=user1)
team2 = Team.objects.create(name="teamdoe2", owner=user1)
Team.objects.create(name="teamdoe3", owner=user1)
Team.objects.create(name="teamdoe4", owner=user1)
team1.members.add(user2)
team2.members.add(user3)
# Personal dataformat not shared with anyone
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user1,
name='personal',
author=user1, name="personal"
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
# Accessible to one user
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user1,
name='public_for_one_user',
author=user1, name="public_for_one_user"
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
dataformat.share(users=[user2])
# Accessible to one team
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user1,
name='public_for_one_team',
author=user1, name="public_for_one_team"
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
dataformat.share(teams=[team1])
# Accessible to teams
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user1,
name='public_for_teams',
author=user1, name="public_for_teams"
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
dataformat.share(teams=[team1, team2])
# Accessible to all
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user1,
name='public_for_all',
author=user1, name="public_for_all"
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
dataformat.share()
def tearDown(self):
if os.path.exists(settings.DATAFORMATS_ROOT):
shutil.rmtree(settings.DATAFORMATS_ROOT)
......@@ -115,146 +112,148 @@ class DataFormatsAPIBase(BaseTestCase):
if os.path.exists(settings.DATAFORMATS_ROOT):
shutil.rmtree(settings.DATAFORMATS_ROOT)
user1 = User.objects.create_user('johndoe', 'johndoe@test.org', '1234')
user2 = User.objects.create_user('jackdoe', 'jackdoe@test.org', '1234')
User.objects.create_user('janedoe', 'janedoe@test.org', '1234')
user4 = User.objects.create_user('jacquelinedoe', 'jacquelinedoe@test.org', '1234')
user1 = User.objects.create_user("johndoe", "johndoe@test.org", TEST_PWD)
user2 = User.objects.create_user("jackdoe", "jackdoe@test.org", TEST_PWD)
User.objects.create_user("janedoe", "janedoe@test.org", TEST_PWD)
user4 = User.objects.create_user(
"jacquelinedoe", "jacquelinedoe@test.org", TEST_PWD
)
team1 = Team.objects.create(name='teamdoe', owner=user2)
Team.objects.create(name='teamdoe2', owner=user2)
team1 = Team.objects.create(name="teamdoe", owner=user2)
Team.objects.create(name="teamdoe2", owner=user2)
team1.members.add(user4)
(dataformat_private, errors) = DataFormat.objects.create_dataformat(
author=user1,
name='format_private',
short_description='short private description',
description='long private description',
name="format_private",
short_description="short private description",
description="long private description",
)
assert dataformat_private, errors
self.assertIsNotNone(dataformat_private, errors)
(dataformat1, errors) = DataFormat.objects.create_dataformat(
author=user1,
name='format1',
short_description='short description 1',
description='long description 1',
name="format1",
short_description="short description 1",
description="long description 1",
)
assert dataformat1, errors
self.assertIsNotNone(dataformat1, errors)
dataformat1.share()
(dataformat2, errors) = DataFormat.objects.create_dataformat(
author=user2,
name='format2',
short_description='short description 2',
description='long description 2',
name="format2",
short_description="short description 2",
description="long description 2",
)
assert dataformat2, errors
self.assertIsNotNone(dataformat2, errors)
(dataformat3, errors) = DataFormat.objects.create_dataformat(
author=user2,
name='format3',
short_description='short description 3',
description='long description 3',
name="format3",
short_description="short description 3",
description="long description 3",
declaration='{\n "#extends": "%s"\n}' % dataformat1.fullname(),
)
assert dataformat3, errors
self.assertIsNotNone(dataformat3, errors)
dataformat3.share()
(dataformat4, errors) = DataFormat.objects.create_dataformat(
author=user2,
name='team_format1',
short_description='team short description 1',
description='team long description 1',
name="team_format1",
short_description="team short description 1",
description="team long description 1",
)
assert dataformat4, errors
self.assertIsNotNone(dataformat4, errors)
dataformat4.share(teams=[team1])
def tearDown(self):
if os.path.exists(settings.DATAFORMATS_ROOT):
shutil.rmtree(settings.DATAFORMATS_ROOT)
def login(self, username):
self.client.login(username=username, password=TEST_PWD)
class DataFormatSharingAPIBase(DataFormatsAPIBase):
def login_johndoe(self):
self.login("johndoe")
def login_jackdoe(self):
self.login("jackdoe")
class DataFormatSharingAPIBase(DataFormatsAPIBase):
def setUp(self):
super(DataFormatSharingAPIBase, self).setUp()
# Users
user1 = User.objects.get(username='jackdoe')
user2 = User.objects.get(username='johndoe')
User.objects.get(username='janedoe')
User.objects.get(username='jacquelinedoe')
user1 = User.objects.get(username="jackdoe")
user2 = User.objects.get(username="johndoe")
User.objects.get(username="janedoe")
User.objects.get(username="jacquelinedoe")
team1 = Team.objects.get(name='teamdoe')
team2 = Team.objects.get(name='teamdoe2')
Team.objects.create(name='teamdoe3', owner=user1)
Team.objects.create(name='teamdoe4', owner=user1)
team1 = Team.objects.get(name="teamdoe")
team2 = Team.objects.get(name="teamdoe2")
Team.objects.create(name="teamdoe3", owner=user1)
Team.objects.create(name="teamdoe4", owner=user1)
# Personal dataformat not shared with anyone
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user1,
name='personal',
author=user1, name="personal"
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
# Accessible to one user
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user1,
name='public_for_one_user',
author=user1, name="public_for_one_user"
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
dataformat.share(users=[user2])
# Accessible to one team
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user1,
name='public_for_one_team',
author=user1, name="public_for_one_team"
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
dataformat.share(teams=[team1])
# Accessible to teams
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user1,
name='public_for_teams',
author=user1, name="public_for_teams"
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
dataformat.share(teams=[team1, team2])
# Accessible to all
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user1,
name='public_for_all',
author=user1, name="public_for_all"
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
dataformat.share()
# Personal dataformat not shared with anyone, extension of a personal one
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user1,
name='extension_of_personal',
name="extension_of_personal",
declaration='{\n "#extends": "%s/personal/1"\n}' % user1.username,
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
# Personal dataformat not shared with anyone, extension of a public one
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user1,
name='extension_of_public',
name="extension_of_public",
declaration='{\n "#extends": "%s/public_for_all/1"\n}' % user1.username,
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
# Personal dataformat not shared with anyone, extension of a limited public one
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user2,
name='extension_of_external',
declaration='{\n "#extends": "%s/public_for_one_user/1"\n}' % user1.username,
name="extension_of_external",
declaration='{\n "#extends": "%s/public_for_one_user/1"\n}'
% user1.username,
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
def checkJSONSharingPreferences(self, sharing_preferences, reference):
self.assertEqual(len(sharing_preferences.keys()), len(reference.keys()))
......
......@@ -33,206 +33,187 @@ from django.test import TestCase
from django.conf import settings
from django.contrib.auth.models import User
from ...common.testutils import tearDownModule
from beat.core.dataformat import Storage, DataFormat as CoreDataFormat
from ...common.testutils import tearDownModule # noqa test runner will call it
from ..models import DataFormat
from .core import TEST_PWD
class DataFormatsCreationFunction(TestCase):
class DataFormatsCreationFunction(TestCase):
def setUp(self):
if os.path.exists(settings.DATAFORMATS_ROOT):
shutil.rmtree(settings.DATAFORMATS_ROOT)
def tearDown(self):
if os.path.exists(settings.DATAFORMATS_ROOT):
shutil.rmtree(settings.DATAFORMATS_ROOT)
def validate(self, dataformat):
storage = Storage(settings.PREFIX, dataformat.fullname())
assert storage.exists
self.assertTrue(storage.exists())
serializer = CoreDataFormat(settings.PREFIX, storage.fullname)
assert serializer.valid, '\n * %s' % '\n * '.join(serializer.errors)
self.assertTrue(serializer.valid, "\n * %s" % "\n * ".join(serializer.errors))
def test_without_declaration(self):
user = User.objects.create_user('johndoe', 'johndoe@test.org', '1234')
user = User.objects.create_user("johndoe", "johndoe@test.org", TEST_PWD)
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user,
name='format1',
short_description='some description',
author=user, name="format1", short_description="some description"
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
db_dataformat = DataFormat.objects.get(author=user, name='format1')
self.assertEqual(db_dataformat.short_description, 'some description')
self.assertEqual(db_dataformat.short_description, 'some description')
db_dataformat = DataFormat.objects.get(author=user, name="format1")
self.assertEqual(db_dataformat.short_description, "some description")
self.assertEqual(db_dataformat.short_description, "some description")
self.assertTrue(db_dataformat.extend is None)
self.assertEqual(db_dataformat.referenced_formats.count(), 0)
self.assertTrue(db_dataformat.modifiable())
self.validate(db_dataformat)
def test_with_declaration(self):
user = User.objects.create_user('johndoe', 'johndoe@test.org', '1234')
user = User.objects.create_user("johndoe", "johndoe@test.org", TEST_PWD)
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user,
name='format1',
short_description='some description',
declaration={
'value': 'int32'
},
name="format1",
short_description="some description",
declaration={"value": "int32"},
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
db_dataformat = DataFormat.objects.get(author=user, name='format1')
self.assertEqual(db_dataformat.short_description, 'some description')
self.assertEqual(db_dataformat.short_description, 'some description')
db_dataformat = DataFormat.objects.get(author=user, name="format1")
self.assertEqual(db_dataformat.short_description, "some description")
self.assertEqual(db_dataformat.short_description, "some description")
self.assertTrue(db_dataformat.extend is None)
self.assertEqual(db_dataformat.referenced_formats.count(), 0)
self.assertTrue(db_dataformat.modifiable())
self.validate(db_dataformat)
def test_extended_format(self):
user = User.objects.create_user('johndoe', 'johndoe@test.org', '1234')
user = User.objects.create_user("johndoe", "johndoe@test.org", TEST_PWD)
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user,
name='format1',
short_description='some description',
declaration={
'value': 'int32'
},
name="format1",
short_description="some description",
declaration={"value": "int32"},
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user,
name='format2',
short_description='some description',
declaration={
'#extends': 'johndoe/format1/1',
'value2': 'int32'
},
name="format2",
short_description="some description",
declaration={"#extends": "johndoe/format1/1", "value2": "int32"},
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
db_dataformat = DataFormat.objects.get(author=user, name='format2')
self.assertEqual(db_dataformat.short_description, 'some description')
self.assertEqual(db_dataformat.short_description, 'some description')
db_dataformat = DataFormat.objects.get(author=user, name="format2")
self.assertEqual(db_dataformat.short_description, "some description")
self.assertEqual(db_dataformat.short_description, "some description")
self.assertTrue(db_dataformat.extend is not None)
self.assertEqual(db_dataformat.extend.name, 'format1')
self.assertEqual(db_dataformat.extend.name, "format1")
self.assertEqual(db_dataformat.referenced_formats.count(), 0)
self.assertTrue(db_dataformat.modifiable())
self.validate(db_dataformat)
def test_composed_format(self):
user = User.objects.create_user('johndoe', 'johndoe@test.org', '1234')
user = User.objects.create_user("johndoe", "johndoe@test.org", TEST_PWD)
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user,
name='format1',
short_description='some description',
declaration={
'value': 'int32'
},
name="format1",
short_description="some description",
declaration={"value": "int32"},
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user,
name='format2',
short_description='some description',
declaration={
'value2': 'int32'
},
name="format2",
short_description="some description",
declaration={"value2": "int32"},
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user,
name='format3',
short_description='some description',
declaration={
'field1': 'johndoe/format1/1',
'field2': 'johndoe/format2/1'
},
name="format3",
short_description="some description",
declaration={"field1": "johndoe/format1/1", "field2": "johndoe/format2/1"},
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
self.assertTrue(dataformat is not None)
db_dataformat = DataFormat.objects.get(author=user, name='format3')
self.assertEqual(db_dataformat.short_description, 'some description')
self.assertEqual(db_dataformat.short_description, 'some description')
db_dataformat = DataFormat.objects.get(author=user, name="format3")
self.assertEqual(db_dataformat.short_description, "some description")
self.assertEqual(db_dataformat.short_description, "some description")
self.assertTrue(db_dataformat.extend is None)
self.assertEqual(db_dataformat.referenced_formats.count(), 2)
self.assertEqual(db_dataformat.referenced_formats.filter(name='format1').count(), 1)
self.assertEqual(db_dataformat.referenced_formats.filter(name='format2').count(), 1)
self.assertEqual(
db_dataformat.referenced_formats.filter(name="format1").count(), 1
)
self.assertEqual(
db_dataformat.referenced_formats.filter(name="format2").count(), 1
)
self.assertTrue(db_dataformat.modifiable())
self.validate(db_dataformat)
def test_extended_composed_format(self):
user = User.objects.create_user('johndoe', 'johndoe@test.org', '1234')
user = User.objects.create_user("johndoe", "johndoe@test.org", TEST_PWD)
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user,
name='format1',
short_description='some description',
declaration={
'value': 'int32'
},
name="format1",
short_description="some description",
declaration={"value": "int32"},
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user,
name='format2',
short_description='some description',
declaration={
'value2': 'int32'
},
name="format2",
short_description="some description",
declaration={"value2": "int32"},
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user,
name='format3',
short_description='some description',
declaration={
'value3': 'int32'
},
name="format3",
short_description="some description",
declaration={"value3": "int32"},
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user,
name='format4',
short_description='some description',
name="format4",
short_description="some description",
declaration={
'#extends': 'johndoe/format3/1',
'field1': 'johndoe/format1/1',
'field2': 'johndoe/format2/1'
"#extends": "johndoe/format3/1",
"field1": "johndoe/format1/1",
"field2": "johndoe/format2/1",
},
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
db_dataformat = DataFormat.objects.get(author=user, name='format4')
self.assertEqual(db_dataformat.short_description, 'some description')
self.assertEqual(db_dataformat.short_description, 'some description')
db_dataformat = DataFormat.objects.get(author=user, name="format4")
self.assertEqual(db_dataformat.short_description, "some description")
self.assertEqual(db_dataformat.short_description, "some description")
self.assertTrue(db_dataformat.extend is not None)