Skip to content

Commit

Permalink
Online Automated Tests, Squashed a couple bugs (#32)
Browse files Browse the repository at this point in the history
* Revise Memberships API wrapper tests

Revise method for generating test accounts; the test accounts need to
be ‘Messaging’ licensed users (to enable the moderator functionality).

* Add TestPeople class to Manage Test Accounts

Add a TestPeople class to create, manage and delete (when tests are
complete) test (person) accounts.

* Simplify pytest.fixture rooms_list and dependencies

Add group_room, direct_rooms, and team_room to the use / dependency
list of fixture rooms_list; remove these dependencies from the tests
that depend on rooms_list

* Change page_size to 1 in test_list_rooms_with_paging

There is no need for each ‘page’ to contain more than a single item.
This may help minimize the number of test rooms created, which will
help minimize the number of API calls that must be made to create and
delete these test rooms.

* Initial TeamsAPI tests

* Generalize test accounts

Remove “group_room_” prefix from test account references to enable them
to be reused by the team_memberships tests.

* Optimize dependencies for authenticated_user_memberships

Add group_room, team_room, and direct_rooms dependencies to
authenticated_user_memberships and remove them from tests that depend
on authenticated_user_memberships.

* Change page_size to 1 in test_list_user_memberships_with_paging

There is no need for each ‘page’ to contain more than a single item.
This may help minimize the number of rooms created, which will
help minimize the number of API calls that must be made to create and
delete these test rooms.

* Optimize use of test accounts when testing deleting memberships

Use a generic / reusable ’named test account’ instead of a
temp_account.  This same test_account can be reused when testing team
memberships.

* Initial TeamMembershipsAPI tests

* Initial PeopleAPI tests

* mentionedPeople should be of type list not string_types

* Add CiscoSparkAPI.people.delete() method / API call

I don’t know how I missed this one…

* Squash some test bugs

* Initial MessagesAPI tests

* Squash memberships test bugs

* Temporarily disable test account deletion

Temporarily disabling test account deletion to work on account
capabilities issues.

* Temp Disable Test:  Update Person API is not working

* Temp Disable Test:  Spark API / documentation bug in GET 'team/memberships'

* Temp Disable Test:  Update team name API is not working
  • Loading branch information
cmlccie authored Mar 17, 2017
1 parent 52b3a2e commit 7ceee94
Show file tree
Hide file tree
Showing 13 changed files with 1,148 additions and 78 deletions.
3 changes: 1 addition & 2 deletions ciscosparkapi/api/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ def list(self, roomId, mentionedPeople=None, before=None,
"""
# Process args
assert isinstance(roomId, string_types)
assert mentionedPeople is None or \
isinstance(mentionedPeople, string_types)
assert mentionedPeople is None or isinstance(mentionedPeople, list)
assert before is None or isinstance(before, string_types)
assert beforeMessage is None or isinstance(beforeMessage, string_types)
assert max is None or isinstance(max, int)
Expand Down
20 changes: 19 additions & 1 deletion ciscosparkapi/api/people.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def create(self, emails, **person_attributes):
"""
# Process args
assert isinstance(emails, string_types) and len(emails) == 1
assert isinstance(emails, list) and len(emails) == 1
post_data = {}
post_data['emails'] = emails
post_data.update(person_attributes)
Expand Down Expand Up @@ -302,6 +302,24 @@ def get(self, personId):
# Return a Person object created from the response JSON data
return Person(json_obj)

def delete(self, personId):
"""Remove a person from the system.
Only an admin can remove a person.
Args:
personId(string_types): The personID of the person.
Raises:
AssertionError: If the parameter types are incorrect.
SparkApiError: If the Cisco Spark cloud returns an error.
"""
# Process args
assert isinstance(personId, string_types)
# API request
self._session.delete('people/' + personId)

def me(self):
"""Get the person details of the account accessing the API 'me'.
Expand Down
63 changes: 63 additions & 0 deletions tests/api/test_licenses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-

"""pytest Licenses API wrapper tests and fixtures."""


import pytest

import ciscosparkapi


# Helper Functions

def get_list_of_licenses(api, orgId=None, max=None):
return api.licenses.list(orgId=orgId, max=max)


def get_license_by_id(api, licenseId):
return api.licenses.get(licenseId)


def is_valid_license(obj):
return isinstance(obj, ciscosparkapi.License) and obj.id is not None


def are_valid_licenses(iterable):
return all([is_valid_license(obj) for obj in iterable])


# pytest Fixtures

@pytest.fixture(scope="session")
def licenses_list(api):
return list(get_list_of_licenses(api))


@pytest.fixture(scope="session")
def licenses_dict(licenses_list):
return {lic.name: lic for lic in licenses_list}


# Tests

class TestLicensesAPI(object):
"""Test LicensesAPI methods."""

def test_list_licenses(self, licenses_list):
assert are_valid_licenses(licenses_list)

def test_list_licenses_with_paging(self, api):
paging_generator = get_list_of_licenses(api, max=1)
licenses = list(paging_generator)
assert licenses > 1
assert are_valid_licenses(licenses)

def test_get_licenses_for_organization(self, api, me):
licenses = list(get_list_of_licenses(api, orgId=me.orgId))
assert are_valid_licenses(licenses)

def test_get_license_by_id(self, api, licenses_list):
assert len(licenses_list) >= 1
license_id = licenses_list[0].id
license = get_license_by_id(api, licenseId=license_id)
assert is_valid_license(license)
228 changes: 210 additions & 18 deletions tests/api/test_memberships.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,237 @@
"""pytest Memberships functions, fixtures and tests."""


import itertools

import pytest

import ciscosparkapi


# Helper Functions

def add_people_to_room(api, room, emails):
for email in emails:
api.memberships.create(room.id, personEmail=email)
def add_person_to_room_by_email(api, room, person, isModerator=False):
return api.memberships.create(room.id,
personEmail=person.emails[0],
isModerator=isModerator)


def add_person_to_room_by_id(api, room, person, isModerator=False):
return api.memberships.create(room.id,
personId=person.id,
isModerator=isModerator)


def add_people_to_room_by_email(api, room, people):
return [add_person_to_room_by_email(api, room, person)
for person in people]


def add_people_to_room_by_id(api, room, people):
return [add_person_to_room_by_id(api, room, person)
for person in people]


def get_room_membership_list(api, room, **kwargs):
return list(api.memberships.list(roomId=room.id, **kwargs))


def get_my_membership(api, room, me):
memberships = get_room_membership_list(api, room, personId=me.id)
assert len(memberships) == 1
membership = memberships[0]
return membership


def get_membership_by_id(api, id):
return api.memberships.get(id)


def make_moderator(api, membership):
return api.memberships.update(membership.id, isModerator=True)


def delete_membership(api, membership):
api.memberships.delete(membership.id)


def empty_room(api, me, room):
"""Remove all memberships from a room (except the caller's membership)."""
"""Remove all memberships from a room (except me)."""
memberships = api.memberships.list(room.id)
for membership in memberships:
if membership.personId != me.id:
api.memberships.delete(membership.id)
delete_membership(api, membership)


def is_valid_membership(membership):
return isinstance(membership, ciscosparkapi.Membership) \
and membership.id is not None


def are_valid_memberships(iterable):
are_valid = (is_valid_membership(item) for item in iterable)
return all(are_valid)


def membership_exists(api, membership):
try:
get_membership_by_id(api, membership.id)
except ciscosparkapi.SparkApiError:
return False
else:
return True


# pytest Fixtures

@pytest.fixture(scope="session")
def group_room_with_members(api, me, group_room, email_addresses):
add_people_to_room(api, group_room, email_addresses)
yield group_room
empty_room(api, me, group_room)
def authenticated_user_memberships(api, group_room, team_room, direct_rooms):
return list(api.memberships.list())


@pytest.fixture(scope="session")
def team_room_with_members(api, me, team_room, email_addresses):
add_people_to_room(api, team_room, email_addresses)
yield team_room
empty_room(api, me, team_room)
def me_group_room_moderator(api, group_room, me):
membership_id = get_my_membership(api, group_room, me)
return make_moderator(api, membership_id)


@pytest.fixture
def temp_group_room_with_members(api, me, temp_group_room, email_addresses):
add_people_to_room(api, temp_group_room, email_addresses)
yield temp_group_room
empty_room(api, me, temp_group_room)
@pytest.fixture(scope="session")
def group_room_member_added_by_email(api, me_group_room_moderator,
group_room, test_people):
person = test_people["member_added_by_email"]
membership = add_person_to_room_by_email(api, group_room, person)

yield membership

delete_membership(api, membership)


@pytest.fixture(scope="session")
def group_room_member_added_by_id(api, me_group_room_moderator,
group_room, test_people):
person = test_people["member_added_by_id"]
membership = add_person_to_room_by_id(api, group_room, person)

yield membership

delete_membership(api, membership)


@pytest.fixture(scope="session")
def group_room_moderator_added_by_email(api, me_group_room_moderator,
group_room, test_people):
person = test_people["moderator_added_by_email"]
membership = add_person_to_room_by_email(api, group_room, person,
isModerator=True)

yield membership

delete_membership(api, membership)


@pytest.fixture(scope="session")
def group_room_moderator_added_by_id(api, me_group_room_moderator,
group_room, test_people):
person = test_people["moderator_added_by_id"]
membership = add_person_to_room_by_id(api, group_room, person,
isModerator=True)

yield membership

delete_membership(api, membership)


@pytest.fixture(scope="session")
def additional_group_room_memberships(group_room_member_added_by_email,
group_room_member_added_by_id,
group_room_moderator_added_by_email,
group_room_moderator_added_by_id):
return [group_room_member_added_by_email,
group_room_member_added_by_id,
group_room_moderator_added_by_email,
group_room_moderator_added_by_id]


@pytest.fixture(scope="session")
def group_room_with_members(group_room, additional_group_room_memberships):
return group_room


# Tests

class TestMembershipsAPI(object):
"""Test MembershipsAPI methods."""

def test_get_membership_details(self, api, me_group_room_moderator):
membership_id = me_group_room_moderator.id
membership = get_membership_by_id(api, membership_id)
assert is_valid_membership(membership)

def test_list_user_memberships(self, authenticated_user_memberships):
assert len(authenticated_user_memberships) >= 3
assert are_valid_memberships(authenticated_user_memberships)

def test_list_user_memberships_with_paging(self, api, add_rooms,
authenticated_user_memberships):
page_size = 1
pages = 3
num_memberships = pages * page_size
if len(authenticated_user_memberships) < num_memberships:
add_rooms(num_memberships - len(authenticated_user_memberships))
memberships = api.memberships.list(max=page_size)
memberships_list = list(itertools.islice(memberships, num_memberships))
assert len(memberships_list) == num_memberships
assert are_valid_memberships(memberships_list)

def test_create_membership_by_email(self,
group_room_member_added_by_email):
assert is_valid_membership(group_room_member_added_by_email)

def test_create_membership_by_person_id(self,
group_room_member_added_by_id):
assert is_valid_membership(group_room_member_added_by_id)

def test_create_moderator_by_email(self,
group_room_moderator_added_by_email):
assert is_valid_membership(group_room_moderator_added_by_email)

def test_create_moderator_by_person_id(self,
group_room_moderator_added_by_id):
assert is_valid_membership(group_room_moderator_added_by_id)

def test_update_membership_make_moderator(self,
me_group_room_moderator):
assert is_valid_membership(me_group_room_moderator)
assert me_group_room_moderator.isModerator

def test_delete_membership(self, api, group_room, test_people):
person = test_people["not_a_member"]
membership = add_person_to_room_by_id(api, group_room, person)
assert is_valid_membership(membership)
delete_membership(api, membership)
assert not membership_exists(api, membership)

def test_list_room_memberships(self, api, group_room_with_members):
memberships = get_room_membership_list(api, group_room_with_members)
assert len(memberships) > 1
assert are_valid_memberships(memberships)

def test_filter_room_memberships_by_personEmail(self, api, test_people,
group_room_with_members):
email = test_people["member_added_by_email"].emails[0]
memberships = get_room_membership_list(api, group_room_with_members,
personEmail=email)
assert len(memberships) == 1
membership = memberships[0]
assert is_valid_membership(membership)
assert membership.roomId == group_room_with_members.id

def test_filter_room_memberships_by_personId(self, api, test_people,
group_room_with_members):
id = test_people["member_added_by_id"].id
memberships = get_room_membership_list(api, group_room_with_members,
personId=id)
assert len(memberships) == 1
membership = memberships[0]
assert is_valid_membership(membership)
assert membership.roomId == group_room_with_members.id
Loading

0 comments on commit 7ceee94

Please sign in to comment.