Commit 4f7419b7 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[libraries][tests] Cleanup

parent f491bc0e
......@@ -36,13 +36,20 @@ from django.conf import settings
from ...dataformats.models import DataFormat
from ...common.testutils import BaseTestCase, tearDownModule
from ...common.testutils import BaseTestCase
from ...common.testutils import ( # noqa test runner will call it, tearDownModule
tearDownModule,
)
from ...team.models import Team
from ..models import Library
from beat.core.library import Storage
TEST_PWD = "1234"
class LibrariesBaseTestCase(BaseTestCase):
class Meta:
model = Library
......@@ -51,29 +58,24 @@ class LibrariesBaseTestCase(BaseTestCase):
if os.path.exists(settings.LIBRARIES_ROOT):
shutil.rmtree(settings.LIBRARIES_ROOT)
def tearDown(self):
if os.path.exists(settings.LIBRARIES_ROOT):
shutil.rmtree(settings.LIBRARIES_ROOT)
class LibrariesCreationFunction(LibrariesBaseTestCase):
def test_without_code(self):
user = User.objects.create_user('johndoe', 'johndoe@test.org', '1234')
user = User.objects.create_user("johndoe", "johndoe@test.org", TEST_PWD)
(library, errors) = Library.objects.create_library(
author=user,
name='library1',
short_description='some description',
author=user, name="library1", short_description="some description"
)
assert library, errors
self.assertIsNotNone(library, errors)
storage = Storage(settings.PREFIX,
library.fullname())
storage.language = 'python'
storage = Storage(settings.PREFIX, library.fullname())
storage.language = "python"
assert storage.exists()
self.assertTrue(storage.exists())
self.assertEqual(library.author, user)
......@@ -81,12 +83,11 @@ class LibrariesCreationFunction(LibrariesBaseTestCase):
self.assertNotEqual(len(declaration), 0)
declaration = json.loads(declaration)
storage.language = declaration['language']
storage.language = declaration["language"]
code = storage.code.try_load()
self.assertNotEqual(len(code), 0)
def test_with_code(self):
declaration = """{
"description": "some description",
......@@ -100,22 +101,18 @@ class LibrariesCreationFunction(LibrariesBaseTestCase):
"""
user = User.objects.create_user('johndoe', 'johndoe@test.org', '1234')
user = User.objects.create_user("johndoe", "johndoe@test.org", TEST_PWD)
(library, errors) = Library.objects.create_library(
author=user,
name='library1',
declaration=declaration,
code=code,
author=user, name="library1", declaration=declaration, code=code
)
assert library, errors
self.assertIsNotNone(library, errors)
storage = Storage(settings.PREFIX,
library.fullname())
storage.language = 'python'
storage = Storage(settings.PREFIX, library.fullname())
storage.language = "python"
assert storage.exists()
self.assertTrue(storage.exists())
self.assertEqual(library.author, user)
self.assertTrue(library.parameters)
......@@ -123,29 +120,36 @@ class LibrariesCreationFunction(LibrariesBaseTestCase):
parameters = json.loads(library.parameters)
self.assertEqual(len(parameters), 1)
self.assertEqual(parameters[0]['name'], 'dummy')
self.assertEqual(parameters[0]["name"], "dummy")
in_storage = json.loads(storage.json.load())
expected = json.loads(declaration)
assert in_storage == expected, "There are differences on the " \
"declaration (beyond white spaces): %r != %r" % \
(in_storage, expected)
self.assertEqual(
in_storage,
expected,
"There are differences on the "
"declaration (beyond white spaces): %r != %r" % (in_storage, expected),
)
# set storage language so it can find the code
storage.language = in_storage['language']
storage.language = in_storage["language"]
in_storage = storage.code.try_load()
expected = code
assert in_storage == expected, "There are differences on the " \
"code code: %r != %r" % (in_storage, expected)
self.assertEqual(
in_storage,
expected,
"There are differences on the "
"code code: %r != %r" % (in_storage, expected),
)
class LibrariesTestCase(LibrariesBaseTestCase):
def setup_libraries(self, declaration, code):
user1 = User.objects.get(username='jackdoe')
user2 = User.objects.get(username='johndoe')
user1 = User.objects.get(username="jackdoe")
user2 = User.objects.get(username="johndoe")
team1 = Team.objects.get(name="teamdoe", owner=user1)
team2 = Team.objects.get(name="teamdoe2", owner=user1)
......@@ -153,70 +157,70 @@ class LibrariesTestCase(LibrariesBaseTestCase):
# Personal
(library, errors) = Library.objects.create_library(
author=user1,
name='personal',
short_description='',
name="personal",
short_description="",
declaration=declaration,
code=code,
)
assert library, errors
self.assertIsNotNone(library, errors)
# Usable by one user
(library, errors) = Library.objects.create_library(
author=user1,
name='usable_by_one_user',
short_description='',
name="usable_by_one_user",
short_description="",
declaration=declaration,
code=code,
)
assert library, errors
self.assertIsNotNone(library, errors)
library.share(public=False, users=[user2])
# Usable by one team
(library, errors) = Library.objects.create_library(
author=user1,
name='usable_by_one_team',
short_description='',
name="usable_by_one_team",
short_description="",
declaration=declaration,
code=code,
)
assert library, errors
self.assertIsNotNone(library, errors)
library.share(public=False, teams=[team1])
# Usable by teams
(library, errors) = Library.objects.create_library(
author=user1,
name='usable_by_teams',
short_description='',
name="usable_by_teams",
short_description="",
declaration=declaration,
code=code,
)
assert library, errors
self.assertIsNotNone(library, errors)
library.share(public=False, teams=[team1, team2])
# Usable by all
(library, errors) = Library.objects.create_library(
author=user1,
name='usable_by_all',
short_description='',
name="usable_by_all",
short_description="",
declaration=declaration,
code=code,
)
assert library, errors
self.assertIsNotNone(library, errors)
library.share(public=False)
# Accessible to one user
(library, errors) = Library.objects.create_library(
author=user1,
name='public_for_one_user',
short_description='',
name="public_for_one_user",
short_description="",
declaration=declaration,
code=code,
)
assert library, errors
self.assertIsNotNone(library, errors)
# library.shared_with.add(user2)
library.share(public=True, users=[user2])
......@@ -224,12 +228,12 @@ class LibrariesTestCase(LibrariesBaseTestCase):
# Accessible to one team
(library, errors) = Library.objects.create_library(
author=user1,
name='public_for_one_team',
short_description='',
name="public_for_one_team",
short_description="",
declaration=declaration,
code=code,
)
assert library, errors
self.assertIsNotNone(library, errors)
# library.shared_with_team.add(team1)
library.share(public=True, teams=[team1])
......@@ -237,30 +241,29 @@ class LibrariesTestCase(LibrariesBaseTestCase):
# Accessible to teams
(library, errors) = Library.objects.create_library(
author=user1,
name='public_for_teams',
short_description='',
name="public_for_teams",
short_description="",
declaration=declaration,
code=code,
)
assert library, errors
self.assertIsNotNone(library, errors)
library.share(public=True, teams=[team1, team2])
# Accessible to all
(library, errors) = Library.objects.create_library(
author=user1,
name='public_for_all',
short_description='',
name="public_for_all",
short_description="",
declaration=declaration,
code=code,
)
assert library, errors
self.assertIsNotNone(library, errors)
library.share(public=True)
class LibrariesAccessibilityFunctionsBase(LibrariesTestCase):
def setUp(self):
super(LibrariesAccessibilityFunctionsBase, self).setUp()
......@@ -274,10 +277,10 @@ class LibrariesAccessibilityFunctionsBase(LibrariesTestCase):
"""
# 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)
......@@ -289,10 +292,9 @@ class LibrariesAccessibilityFunctionsBase(LibrariesTestCase):
# Create a public dataformat
(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()
self.setup_libraries(declaration, code)
......@@ -317,15 +319,14 @@ class LibrariesAPIBase(LibrariesTestCase):
"language": "python"
}"""
def setUp(self):
super(LibrariesAPIBase, self).setUp()
# 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", "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")
team1 = Team.objects.create(name="teamdoe", owner=user1)
team2 = Team.objects.create(name="teamdoe2", owner=user1)
......@@ -337,10 +338,9 @@ class LibrariesAPIBase(LibrariesTestCase):
# Create a public dataformat
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=user2,
name='single_integer',
author=user2, name="single_integer"
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
dataformat.share()
self.setup_libraries(self.DECLARATION, LibrariesAPIBase.CODE)
......@@ -348,13 +348,22 @@ class LibrariesAPIBase(LibrariesTestCase):
# Private fork
(library, errors) = Library.objects.create_library(
author=user2,
name='forked_algo',
short_description='',
name="forked_algo",
short_description="",
declaration=LibrariesAPIBase.DECLARATION,
code=LibrariesAPIBase.CODE,
)
assert library, errors
self.assertIsNotNone(library, errors)
library.previous_version = None
library.fork_of = Library.objects.get(name='public_for_all')
library.fork_of = Library.objects.get(name="public_for_all")
library.save()
def login(self, username):
self.client.login(username=username, password=TEST_PWD)
def login_johndoe(self):
self.login("johndoe")
def login_jackdoe(self):
self.login("jackdoe")
......@@ -34,8 +34,10 @@ from django.core.urlresolvers import reverse
import beat.core.library
from ...common.testutils import tearDownModule
from ...common.testutils import get_algorithms_from_data
from ...common.testutils import ( # noqa test runner will call it, tearDownModule
tearDownModule,
)
from ..models import Library
......@@ -46,323 +48,323 @@ class LibrariesListRetrieval(LibrariesAPIBase):
def setUp(self):
super(LibrariesListRetrieval, self).setUp()
self.url = reverse('api_libraries:all')
self.own_url = reverse('api_libraries:list_create', args=['jackdoe'])
self.url = reverse("api_libraries:all")
self.own_url = reverse("api_libraries:list_create", args=["jackdoe"])
def test_only_public_and_usable_libraries_for_anonymous_user(self):
response = self.client.get(self.url)
content = self.checkResponse(response, 200, content_type='application/json')
content = self.checkResponse(response, 200, content_type="application/json")
self.assertTrue(isinstance(content, list))
self.assertEqual(len(content), 2)
library = get_algorithms_from_data(content, 'jackdoe/public_for_all/1')[0]
self.assertEqual(library['short_description'], '')
self.assertTrue(library['fork_of'] is None)
self.assertEqual(library['accessibility'], 'public')
self.assertFalse('is_owner' in library)
library = get_algorithms_from_data(content, 'jackdoe/usable_by_all/1')[0]
self.assertEqual(library['short_description'], '')
self.assertTrue(library['fork_of'] is None)
self.assertEqual(library['accessibility'], 'confidential')
self.assertFalse('is_owner' in library)
library = get_algorithms_from_data(content, "jackdoe/public_for_all/1")[0]
self.assertEqual(library["short_description"], "")
self.assertTrue(library["fork_of"] is None)
self.assertEqual(library["accessibility"], "public")
self.assertFalse("is_owner" in library)
library = get_algorithms_from_data(content, "jackdoe/usable_by_all/1")[0]
self.assertEqual(library["short_description"], "")
self.assertTrue(library["fork_of"] is None)
self.assertEqual(library["accessibility"], "confidential")
self.assertFalse("is_owner" in library)
def test_all_accessible_libraries(self):
self.client.login(username='johndoe', password='1234')
self.login_johndoe()
response = self.client.get(self.url)
content = self.checkResponse(response, 200, content_type='application/json')
content = self.checkResponse(response, 200, content_type="application/json")
self.assertTrue(isinstance(content, list))
self.assertEqual(len(content), 9)
library = get_algorithms_from_data(content, 'jackdoe/usable_by_one_user/1')[0]
self.assertEqual(library['short_description'], '')
self.assertEqual(library['version'], 1)
self.assertEqual(library['previous_version'], None)
self.assertTrue(library['fork_of'] is None)
self.assertEqual(library['is_owner'], False)
self.assertEqual(library['accessibility'], 'confidential')
library = get_algorithms_from_data(content, 'jackdoe/usable_by_one_team/1')[0]
self.assertEqual(library['short_description'], '')
self.assertEqual(library['version'], 1)
self.assertEqual(library['previous_version'], None)
self.assertTrue(library['fork_of'] is None)
self.assertEqual(library['is_owner'], False)
self.assertEqual(library['accessibility'], 'confidential')
library = get_algorithms_from_data(content, 'jackdoe/usable_by_teams/1')[0]
self.assertEqual(library['short_description'], '')
self.assertEqual(library['version'], 1)
self.assertEqual(library['previous_version'], None)
self.assertTrue(library['fork_of'] is None)
self.assertEqual(library['is_owner'], False)
self.assertEqual(library['accessibility'], 'confidential')
library = get_algorithms_from_data(content, 'jackdoe/public_for_all/1')[0]
self.assertEqual(library['short_description'], '')
self.assertEqual(library['version'], 1)
self.assertEqual(library['previous_version'], None)
self.assertTrue(library['fork_of'] is None)
self.assertEqual(library['is_owner'], False)
self.assertEqual(library['accessibility'], 'public')
library = get_algorithms_from_data(content, 'jackdoe/usable_by_all/1')[0]
self.assertEqual(library['short_description'], '')
self.assertEqual(library['version'], 1)
self.assertEqual(library['previous_version'], None)
self.assertTrue(library['fork_of'] is None)
self.assertEqual(library['is_owner'], False)
self.assertEqual(library['accessibility'], 'confidential')
library = get_algorithms_from_data(content, 'jackdoe/public_for_one_user/1')[0]
self.assertEqual(library['short_description'], '')
self.assertEqual(library['version'], 1)
self.assertEqual(library['previous_version'], None)
self.assertTrue(library['fork_of'] is None)
self.assertEqual(library['is_owner'], False)
self.assertEqual(library['accessibility'], 'confidential')
library = get_algorithms_from_data(content, 'jackdoe/public_for_one_team/1')[0]
self.assertEqual(library['short_description'], '')
self.assertEqual(library['version'], 1)
self.assertEqual(library['previous_version'], None)
self.assertTrue(library['fork_of'] is None)
self.assertEqual(library['is_owner'], False)
self.assertEqual(library['accessibility'], 'confidential')
library = get_algorithms_from_data(content, 'jackdoe/public_for_teams/1')[0]
self.assertEqual(library['short_description'], '')
self.assertEqual(library['version'], 1)
self.assertEqual(library['previous_version'], None)
self.assertTrue(library['fork_of'] is None)
self.assertEqual(library['is_owner'], False)
self.assertEqual(library['accessibility'], 'confidential')
library = get_algorithms_from_data(content, 'johndoe/forked_algo/1')[0]
self.assertEqual(library['short_description'], '')
self.assertEqual(library['version'], 1)
self.assertTrue(library['previous_version'] is None)
self.assertEqual(library['fork_of'], 'jackdoe/public_for_all/1')
self.assertEqual(library['is_owner'], True)
self.assertEqual(library['accessibility'], 'private')
library = get_algorithms_from_data(content, "jackdoe/usable_by_one_user/1")[0]
self.assertEqual(library["short_description"], "")
self.assertEqual(library["version"], 1)
self.assertEqual(library["previous_version"], None)
self.assertTrue(library["fork_of"] is None)
self.assertEqual(library["is_owner"], False)
self.assertEqual(library["accessibility"], "confidential")
library = get_algorithms_from_data(content, "jackdoe/usable_by_one_team/1")[0]
self.assertEqual(library["short_description"], "")
self.assertEqual(library["version"], 1)
self.assertEqual(library["previous_version"], None)
self.assertTrue(library["fork_of"] is None)
self.assertEqual(library["is_owner"], False)
self.assertEqual(library["accessibility"], "confidential")
library = get_algorithms_from_data(content, "jackdoe/usable_by_teams/1")[0]
self.assertEqual(library["short_description"], "")
self.assertEqual(library["version"], 1)
self.assertEqual(library["previous_version"], None)
self.assertTrue(library["fork_of"] is None)
self.assertEqual(library["is_owner"], False)
self.assertEqual(library["accessibility"], "confidential")
library = get_algorithms_from_data(content, "jackdoe/public_for_all/1")[0]
self.assertEqual(library["short_description"], "")
self.assertEqual(library["version"], 1)
self.assertEqual(library["previous_version"], None)
self.assertTrue(library["fork_of"] is None)
self.assertEqual(library["is_owner"], False)
self.assertEqual(library["accessibility"], "public")
library = get_algorithms_from_data(content, "jackdoe/usable_by_all/1")[0]
self.assertEqual(library["short_description"], "")
self.assertEqual(library["version"], 1)
self.assertEqual(library["previous_version"], None)
self.assertTrue(library["fork_of"] is None)
self.assertEqual(library["is_owner"], False)
self.assertEqual(library["accessibility"], "confidential")
library = get_algorithms_from_data(content, "jackdoe/public_for_one_user/1")[0]
self.assertEqual(library["short_description"], "")
self.assertEqual(library["version"], 1)
self.assertEqual(library["previous_version"], None)
self.assertTrue(library["fork_of"] is None)
self.assertEqual(library["is_owner"], False)
self.assertEqual(library["accessibility"], "confidential")
library = get_algorithms_from_data(content, "jackdoe/public_for_one_team/1")[0]
self.assertEqual(library["short_description"], "")
self.assertEqual(library["version"], 1)
self.assertEqual(library["previous_version"], None)
self.assertTrue(library["fork_of"] is None)
self.assertEqual(library["is_owner"], False)
self.assertEqual(library["accessibility"], "confidential")
library = get_algorithms_from_data(content, "jackdoe/public_for_teams/1")[0]
self.assertEqual(library["short_description"], "")
self.assertEqual(library["version"], 1)
self.assertEqual(library["previous_version"], None)
self.assertTrue(library["fork_of"] is None)
self.assertEqual(library["is_owner"], False)
self.assertEqual(library["accessibility"], "confidential")
library = get_algorithms_from_data(content, "johndoe/forked_algo/1")[0]
self.assertEqual(library["short_description"], "")
self.assertEqual(library["version"], 1)
self.assertTrue(library["previous_version"] is None)
self.assertEqual(library["fork_of"], "jackdoe/public_for_all/1")
self.assertEqual(library["is_owner"], True)
self.assertEqual(library["accessibility"], "private")
def test_own_libraries(self):
self.client.login(username='jackdoe', password='1234')
self.login_jackdoe()
response = self.client.get(self.own_url)
content = self.checkResponse(response, 200, content_type='application/json')
content = self.checkResponse(response, 200, content_type="application/json")
self.assertTrue(isinstance(content, list))