Verified Commit 069527dc authored by Peter Stanko's avatar Peter Stanko
Browse files

Roles and Groups testing

parent 11dfccac
Loading
Loading
Loading
Loading
+17 −6
Original line number Diff line number Diff line
@@ -17,13 +17,13 @@ class UserAddsMixin(object):
        response = self.rest.put(url, {'add': user_selectors})
        return self._make_instance(response)

    def delete_user(self, entity_id, user):
    def remove_user(self, entity_id, user):
        user_selector = get_selector(user)
        url = f"{self.url}/{entity_id}/users/{user_selector}"
        self.log.info(f"[DEL-USER] {self._instance_klass.__name__} ({url}): {user}")
        self.rest.delete(url)

    def delete_users(self, entity_id, *users):
    def remove_users(self, entity_id, *users):
        user_selectors = [get_selector(user) for user in users]
        url = f"{self.url}/{entity_id}/users/"
        self.log.info(f"[DEL-USERS] {self._instance_klass.__name__} ({url}): {user_selectors}")
@@ -38,6 +38,11 @@ class UserAddMixin(object):
    def add_users(self, *users):
        return self.client.add_users(self.entity_id, *users)

    def remove_user(self, user):
        return self.client.remove_user(self.entity_id, user)

    def remove_users(self, *users):
        return self.client.remove_user(self.entity_id, *users)

class ProjectAddsMixin(object):
    def add_project(self, entity_id, project):
@@ -55,13 +60,13 @@ class ProjectAddsMixin(object):
        response = self.rest.put(url, {'add': projects_selectors})
        return self._make_instance(response)

    def delete_project(self, entity_id, projects):
    def remove_project(self, entity_id, projects):
        user_selector = get_selector(projects)
        url = f"{self.url}/{entity_id}/projects/{user_selector}"
        self.log.info(f"[DEL-PROJECT] {self._instance_klass.__name__} ({url}): {projects}")
        self.rest.delete(url)

    def delete_projects(self, entity_id, *projects):
    def remove_projects(self, entity_id, *projects):
        projects_selectors = [get_selector(user) for user in projects]
        url = f"{self.url}/{entity_id}/projects/"
        self.log.info(f"[DEL-PROJECT] {self._instance_klass.__name__} ({url}): {projects_selectors}")
@@ -70,8 +75,14 @@ class ProjectAddsMixin(object):


class ProjectAddMixin(object):
    def add_user(self, project):
    def add_project(self, project):
        return self.client.add_project(self.entity_id, project)

    def add_users(self, *projects):
    def add_projects(self, *projects):
        return self.client.add_projects(self.entity_id, *projects)

    def remove_project(self, project):
        return self.client.remove_project(self.entity_id, project)

    def remove_projects(self, *projects):
        return self.client.remove_projects(self.entity_id, *projects)
+24 −17
Original line number Diff line number Diff line
@@ -3,13 +3,14 @@ import random
from secrets import token_urlsafe

from kontr_api import KontrClient
from kontr_api.resources import User, Course, Component, Project
from kontr_api.resources import User, Course, Component, Project, Role, Group

log = logging.getLogger(__name__)


def unique_name(name: str):
    return name + '_' + token_urlsafe(8)
    urlsafe = token_urlsafe(5).replace('_', '')
    return (name + '-' + urlsafe).lower()


class EntitiesCreator(object):
@@ -18,7 +19,7 @@ class EntitiesCreator(object):
        self.entities_stack = []

    def get_user_config(self, **kwargs) -> dict:
        uname = unique_name('test_user')
        uname = unique_name('test-user')
        config = dict(username=uname,
                      email=f"{uname}@example.com",
                      name='Test User',
@@ -28,7 +29,7 @@ class EntitiesCreator(object):
        return config

    def get_course_config(self, **kwargs) -> dict:
        uname = unique_name('test_course')
        uname = unique_name('test-course')
        config = dict(name=uname.capitalize(),
                      codename=uname,
                      description=f'Created Course: {uname}')
@@ -37,7 +38,7 @@ class EntitiesCreator(object):
        return config

    def get_component_config(self, **kwargs) -> dict:
        uname = unique_name('test_component')
        uname = unique_name('test-component')
        config = dict(name=uname.capitalize(),
                      secret=uname + "-secret",
                      ip_address='127.0.0.1',
@@ -53,16 +54,16 @@ class EntitiesCreator(object):
        return config

    def get_project_config(self, **kwargs) -> dict:
        uname = unique_name('test_project')
        uname = unique_name('test-project')
        config = dict(name=uname.capitalize(),
                      codename=uname,
                      description=f"Created Project: {uname}",
                      config=self.get_project_config_config())
                      assignment_url='https://cecko.eu',
                      description=f"Created Project: {uname}")
        config.update(kwargs)
        return config

    def get_role_config(self, **kwargs) -> dict:
        uname = unique_name('test_role')
        uname = unique_name('test-role')
        config = dict(name=uname.capitalize(),
                      codename=uname,
                      description=f"Created Role: {uname}")
@@ -70,7 +71,7 @@ class EntitiesCreator(object):
        return config

    def get_group_config(self, **kwargs) -> dict:
        uname = unique_name('test_group')
        uname = unique_name('test-group')
        config = dict(name=uname.capitalize(),
                      codename=uname,
                      description=f"Created Group: {uname}")
@@ -90,17 +91,13 @@ class EntitiesCreator(object):
        course = course or self.create_course()
        return self.create_any('project', course.projects.create, **kwargs)

    def create_role(self, course=None, **kwargs) -> Project:
    def create_role(self, course=None, **kwargs) -> Role:
        course = course or self.create_course()
        return self.create_any('role', course.roles.create, **kwargs)

    def create_group(self, course=None, **kwargs) -> Project:
    def create_group(self, course=None, **kwargs) -> Group:
        course = course or self.create_course()
        return self.create_any('role', course.groups.create, **kwargs)

    def create_submission(self, course=None, project=None, **kwargs) -> Project:
        project = project or self.create_project(course=course)
        return self.create_any('role', project.submissions().create, **kwargs)
        return self.create_any('group', course.groups.create, **kwargs)

    def create_any(self, resource=None, create_method=None, **kwargs):
        conf_method = getattr(self, f"get_{resource}_config")
@@ -122,3 +119,13 @@ def assert_params(expected, entity, params=None):
    params = params or expected.keys()
    for param in params:
        assert expected[param] == entity[param]


def assert_not_in_collection(entity_testing, user_entity, collection):
    collection_ids = [item['id'] for item in entity_testing[collection]]
    assert user_entity.entity_id not in collection_ids


def assert_in_collection(entity_testing, user_entity, collection):
    collection_ids = [item['id'] for item in entity_testing[collection]]
    assert user_entity.entity_id in collection_ids
 No newline at end of file
+90 −0
Original line number Diff line number Diff line
import pytest

from kontr_api.resources import Course, Project, User, Group
from tests.integration.helpers import EntitiesCreator, assert_params, assert_not_in_collection, \
    assert_in_collection


@pytest.fixture()
def course_testing(ent_creator: EntitiesCreator) -> Course:
    entity = ent_creator.create_course()
    return entity


@pytest.fixture()
def entity_testing(ent_creator: EntitiesCreator, course_testing: Course) -> Group:
    return ent_creator.create_group(course=course_testing)


@pytest.fixture()
def entity_client(course_testing: Course):
    return course_testing.groups


@pytest.fixture()
def project_entity(ent_creator: EntitiesCreator, course_testing) -> Project:
    return ent_creator.create_project(course=course_testing)


def test_groups_has_been_created(entity_client, ent_creator: EntitiesCreator):
    entity_config = ent_creator.get_role_config()
    created = entity_client.create(entity_config)
    assert_params(expected=entity_config, entity=created, params=['name', 'description'])
    ent_creator.entities_stack.append(created)


def test_groups_should_be_listed(entity_client, entity_testing):
    entities = entity_client.list()
    assert len(entities) > 0
    assert entity_testing in entities


def test_groups_should_be_updated(entity_client, entity_testing):
    param = 'description'
    old_name = entity_testing[param]
    new_name = old_name + " Updated"
    entity_testing[param] = new_name
    entity_testing.update()
    assert entity_testing[param] == new_name


def test_groups_should_be_deleted(entity_client, entity_testing):
    assert entity_testing in entity_client.list()
    entity_testing.delete()
    assert entity_testing not in entity_client.list()

@pytest.fixture()
def user_entity(ent_creator: EntitiesCreator) -> User:
    return ent_creator.create_user()


def test_groups_add_user(entity_client, entity_testing, user_entity):
    assert_not_in_collection(entity_testing, user_entity, 'users')
    entity_testing.add_user(user_entity)
    entity_testing.read()
    assert_in_collection(entity_testing, user_entity, 'users')


def test_groups_del_user(entity_client, entity_testing, user_entity):
    entity_testing.add_user(user_entity)
    entity_testing.read()
    assert_in_collection(entity_testing, user_entity, 'users')
    entity_testing.remove_user(user_entity)
    entity_testing.read()
    assert_not_in_collection(entity_testing, user_entity, 'users')


def test_groups_add_project(entity_client, entity_testing, project_entity):
    assert_not_in_collection(entity_testing, project_entity, 'projects')
    entity_testing.add_project(project_entity)
    entity_testing.read()
    assert_in_collection(entity_testing, project_entity, 'projects')


def test_groups_del_project(entity_client, entity_testing, project_entity):
    entity_testing.add_project(project_entity)
    entity_testing.read()
    assert_in_collection(entity_testing, project_entity, 'projects')
    entity_testing.remove_project(project_entity)
    entity_testing.read()
    assert_not_in_collection(entity_testing, project_entity, 'projects')
+70 −0
Original line number Diff line number Diff line
import pytest

from kontr_api.resources import Course, Project, User
from tests.integration.helpers import EntitiesCreator, assert_params, assert_in_collection, \
    assert_not_in_collection


@pytest.fixture()
def course_testing(ent_creator: EntitiesCreator) -> Course:
    entity = ent_creator.create_course()
    return entity


@pytest.fixture()
def entity_testing(ent_creator: EntitiesCreator, course_testing: Course) -> Project:
    return ent_creator.create_role(course=course_testing)


@pytest.fixture()
def entity_client(course_testing: Course):
    return course_testing.roles


def test_roles_has_been_created(entity_client, ent_creator: EntitiesCreator):
    entity_config = ent_creator.get_role_config()
    created = entity_client.create(entity_config)
    assert_params(expected=entity_config, entity=created, params=['name', 'description'])
    ent_creator.entities_stack.append(created)


def test_roles_should_be_listed(entity_client, entity_testing):
    entities = entity_client.list()
    assert len(entities) > 0
    assert entity_testing in entities


def test_roles_should_be_updated(entity_client, entity_testing):
    param = 'description'
    old_name = entity_testing[param]
    new_name = old_name + " Updated"
    entity_testing[param] = new_name
    entity_testing.update()
    assert entity_testing[param] == new_name


def test_roles_should_be_deleted(entity_client, entity_testing):
    assert entity_testing in entity_client.list()
    entity_testing.delete()
    assert entity_testing not in entity_client.list()


@pytest.fixture()
def user_entity(ent_creator: EntitiesCreator) -> User:
    return ent_creator.create_user()


def test_groups_add_user(entity_client, entity_testing, user_entity):
    assert_not_in_collection(entity_testing, user_entity, 'users')
    entity_testing.add_user(user_entity)
    entity_testing.read()
    assert_in_collection(entity_testing, user_entity, 'users')


def test_groups_del_user(entity_client, entity_testing, user_entity):
    entity_testing.add_user(user_entity)
    entity_testing.read()
    assert_in_collection(entity_testing, user_entity, 'users')
    entity_testing.remove_user(user_entity)
    entity_testing.read()
    assert_not_in_collection(entity_testing, user_entity, 'users')