Exemplo n.º 1
0
    def setUp(self):
        self.ae = MockAppEngine()
        self.user = self.ae.get_current_user()
        self.db = MockAccess(user=self.user, add_owner=self.is_user_owner())

        # set up mock Request and Response objects
        self.request = mock.MagicMock()
        self.response = mock.MagicMock()

        # Create a special SpyResource class that has all the same
        # functionality as the appropriate Resource for this test, but
        # has a fake 'dump' method which stores the last js_object dumped
        resource_cls = self.get_resource_cls()

        class SpyResource(resource_cls):
            def __init__(self, *args, **kwargs):
                super(SpyResource, self).__init__(*args, **kwargs)
                self.last_response = None

            def get_last_response(self):
                return self.last_response

            def dump(self, js_response):
                self.last_response = js_response

        # instantiate the SpyResource with fake Request/Response as well as
        # AppEngine and DbAccess
        self.resource = SpyResource(
            request=self.request,
            response=self.response,
            ae=self.ae,
            db=self.db,
        )
Exemplo n.º 2
0
 def setUp(self):
     self.db = MockAccess(User(), add_owner=True)
     self.ae = MockWrapper()
     self.rpc = ListRpcGroup(self.db, self.ae)
Exemplo n.º 3
0
class ListRpcTest(unittest.TestCase):
    def setUp(self):
        self.db = MockAccess(User(), add_owner=True)
        self.ae = MockWrapper()
        self.rpc = ListRpcGroup(self.db, self.ae)

    def set_user(self, user):
        self.db.user = user

    def set_owner(self, owner):
        self.db.owner = owner
        self.db.user = owner.user

    def delete_owner(self):
        oid = self.db.owner.key().id()
        self.db.delete(self.db.owner)
        self.db.owner = None
        return oid

    def create_group(self, created_by, members, name='Group', desc='Desc'):
        g = self.db.add_group(name, desc, created_by)
        [self.db.add_group_member(g, o) for o in members]
        return g

    def create_list(self, oid, idx, count):
        _ = lambda x: '{} {}'.format(x, idx)
        wl = self.db.add_list(oid, _('List'), _('List Desc'))
        lid = wl.key().id()
        for idx in xrange(count):
            self.db.add_list_item(lid, _('Item'), _('Cat'), _('Desc'),
                                  _('url'), idx % 2 == 0)

    def create_lists(self, owner, count):
        oid = owner.key().id()
        for i in xrange(count):
            self.create_list(oid, i, 2 * i)

    def compare_all_lists(self, lists, wlists):
        self.assertEqual(len(lists), len(wlists))
        for l, wl in zip(lists, wlists):
            self.compare_lists(l, wl)

    def compare_lists(self, l, wl):
        l_items, wl_items = (l.items, wl.items)
        self.assertEqual(len(l_items), len(wl_items))
        for item, witem in zip(l_items, wl_items):
            self.compare_items(item, witem)

    def compare_items(self, item, witem):
        ga, eq = (getattr, self.assertEqual)
        _ = lambda x: x.label() if x is not None else ''
        attrs = ('name', 'category', 'description', 'url', 'is_surprise')
        [eq(ga(item, a), ga(witem, a)) for a in attrs]
        eq(_(item.purchased_by), witem.purchased_by)
        eq(_(item.reserved_by), witem.reserved_by)

    def test_add_list(self):
        '''
        Confirm that add_list actually adds a list
        '''
        oid = self.db.owner.key().id()
        self.rpc.add_list(oid, 'List Name', 'List Desc')
        self.assertEqual(1, len(self.db.lists))
        l = self.db.lists.values()[0]
        self.assertTrue(l.key().id() in range(1, 1001))
        self.assertEqual('List Name', l.name)
        self.assertEqual('List Desc', l.description)

    def test_add_list_no_owner(self):
        '''
        Confirm that trying to add a list with no owner raises a
        PermissionDeniedError
        '''
        oid = self.delete_owner()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.add_list(oid, 'Name', 'Desc')

    def test_add_list_other_owner(self):
        '''
        Confirm that trying to add a list for another owner raises a
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        oid = o.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.add_list(oid, 'Name', 'Desc')

    def test_add_list_duplicate_name(self):
        '''
        Confirm that trying to add a list with a duplicate name raises
        a DuplicateNameError
        '''
        oid = self.db.owner.key().id()
        name = 'My Wish List'
        self.db.add_list(oid, name, 'Description')
        with self.assertRaises(DuplicateNameError):
            self.rpc.add_list(oid, name, 'Another List Description')

    def test_update_list(self):
        '''
        Confirm that update_list actually updates a list
        '''
        oid = self.db.owner.key().id()
        l = self.db.add_list(oid, 'Name', 'Description')
        lid = l.key().id()
        self.rpc.update_list(lid, 'New Name', 'New Desc')
        self.assertEqual(1, len(self.db.lists))
        l = self.db.lists.values()[0]
        self.assertEqual('New Name', l.name)
        self.assertEqual('New Desc', l.description)

    def test_update_list_no_owner(self):
        '''
        Confirm that trying to update a list with no owner raises a
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        l = self.db.add_list(o.key().id(), 'List Name', 'List Desc')
        lid = l.key().id()
        self.delete_owner()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.update_list(lid, 'Name', 'Desc')

    def test_update_list_invalid_owner(self):
        '''
        Confirm that trying to add a list for an invalid owner raises a
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        oid = o.key().id()
        l = self.db.add_list(oid, 'New List', 'New Desc')
        lid = l.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.update_list(lid, 'Name', 'Desc')

    def test_update_list_duplicate_name(self):
        '''
        Confirm that trying to update a list with a duplicate name raises
        a DuplicateNameError
        '''
        oid = self.db.owner.key().id()
        name = 'My Wish List'
        self.db.add_list(oid, name, 'Description')
        l = self.db.add_list(oid, 'Second Name', 'Second Description')
        lid = l.key().id()
        with self.assertRaises(DuplicateNameError):
            self.rpc.update_list(lid, name, 'Another List Description')

    def test_get_list_no_owner(self):
        '''
        Confirm that trying to get lists  with no owner raises a
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        oid = o.key().id()
        self.delete_owner()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.get_lists(oid)

    def test_get_lists_invalid_owner(self):
        '''
        Confirm that trying to get lists for an invalid owner raises
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        oid = o.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.get_lists(oid)

    def test_get_own_lists(self):
        '''
        Confirm that get_lists works for current owner
        '''
        oid = self.db.owner.key().id()

        def _add(i):
            return self.db.add_list(oid, 'List {}'.format(i),
                                    'Desc {}'.format(i))

        lists = [_add(i) for i in '12345']
        wls = self.rpc.get_lists(oid)
        self.assertEqual(len(lists), len(wls))

    def test_get_lists_from_member_of_my_group(self):
        '''
        Confirm that get_lists works for an owner who is a member of
        a group owned by current owner
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(self.db.owner, [o])
        oid = o.key().id()
        wls = self.rpc.get_lists(oid)
        self.compare_all_lists(o.lists, wls)

    def test_get_lists_from_member_of_group_im_in(self):
        '''
        Confirm that get_lists works for an owner who is a member of
        a group current owner is also in
        '''
        o1 = self.db.add_owner(User('foo', '*****@*****.**'))
        o2 = self.db.add_owner(User('bar', '*****@*****.**'))
        self.create_lists(o1, 5)
        self.create_group(o2, [o1, self.db.owner])
        oid = o1.key().id()
        wls = self.rpc.get_lists(oid)
        self.compare_all_lists(o1.lists, wls)
        pass

    def test_get_lists_from_owner_of_group_im_in(self):
        '''
        Confirm that get_lists works for an owner who is the owner of
        a group current owner is in
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        oid = o.key().id()
        wls = self.rpc.get_lists(oid)
        self.compare_all_lists(o.lists, wls)

    def test_add_item_to_invalid_owner(self):
        '''
        Confirm that trying to add an item to the list of an invalid owner
        raises PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        lid = o.lists[0].key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.add_item(lid, 'item', 'cat', 'desc', 'url', False)

    def test_add_surprise_item_to_invalid_owner(self):
        '''
        Confirm that trying to add a surprise item to the list of an invalid
        owner raises PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        lid = o.lists[0].key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.add_item(lid, 'item', 'cat', 'desc', 'url', True)

    def test_add_surprise_item_for_fellow_group_memeber(self):
        '''
        Confirm ability to add a surprise item to a list you have access to
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        l = o.lists[2]
        count = len(l.items)
        lid = l.key().id()
        w_list = self.rpc.add_item(lid, 'item', 'cat', 'desc', 'url', True)
        witem = w_list.items[-1]
        self.assertEqual(count + 1, len(o.lists[2].items))
        self.assertEqual('item', witem.name)
        self.assertEqual('cat', witem.category)
        self.assertEqual('desc', witem.description)
        self.assertEqual('url', witem.url)
        self.assertEqual(True, witem.is_surprise)
        self.assertEqual(self.db.owner.nickname, witem.reserved_by)

    def test_add_item_to_own_list(self):
        '''
        Confirm ability to add an item to your own list
        '''
        self.create_lists(self.db.owner, 1)
        l = self.db.owner.lists[0]
        count = len(l.items)
        lid = l.key().id()
        w_list = self.rpc.add_item(lid, 'item', 'cat', 'desc', 'url', False)
        witem = w_list.items[0]
        self.assertEqual(count + 1, len(self.db.owner.lists[0].items))
        self.assertEqual('item', witem.name)
        self.assertEqual('cat', witem.category)
        self.assertEqual('desc', witem.description)
        self.assertEqual('url', witem.url)
        self.assertEqual(False, witem.is_surprise)
        self.assertEqual('', witem.reserved_by)
        self.assertEqual('', witem.purchased_by)

    def test_update_item_in_own_list(self):
        '''
        Confirm ability to update an existing item in your own list
        '''
        self.create_lists(self.db.owner, 2)
        l = self.db.owner.lists[1]
        count = len(l.items)
        item = l.items[0]
        iid = item.key().id()
        n, c, d, u = ('New Name', 'New Cat', 'New Desc', 'New URL')
        w_list = self.rpc.update_item(iid, n, c, d, u)
        witem = w_list.items[0]
        self.assertEqual(count, len(self.db.owner.lists[1].items))
        self.assertEqual(n, witem.name)
        self.assertEqual(c, witem.category)
        self.assertEqual(d, witem.description)
        self.assertEqual(u, witem.url)

    def test_update_item_in_invalid_list(self):
        ''''
        Confirm that trying to update an item in someone else's list
        raises PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[2].items[1]
        iid = item.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.update_item(iid, 'item', 'cat', 'desc', 'url')

    def test_remove_item_from_own_list(self):
        '''
        Confirm ability to remove an item from your own list
        '''
        self.create_lists(self.db.owner, 5)
        l = self.db.owner.lists[3]
        count = len(l.items)
        item = l.items[0]
        iid = item.key().id()
        self.rpc.remove_item(iid)
        self.assertEqual(count - 1, len(self.db.owner.lists[3].items))
        self.assertTrue(item not in self.db.owner.lists[3].items)

    def test_remove_reserved_item_from_own_list(self):
        '''
        Confirm ability to remove an item from your own list
        which has previously been marked as reserved, and that an
        email is sent to the reserver
        '''
        self.create_lists(self.db.owner, 5)
        l = self.db.owner.lists[-1]
        count = len(l.items)
        item = l.items[0]
        iid = item.key().id()
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        item.reserved_by = o
        item.put()
        self.rpc.remove_item(iid)
        self.assertEqual(count - 1, len(self.db.owner.lists[-1].items))
        owner = self.db.owner
        subject = 'Wish List Item Deleted'
        template_tuple = (
            item.reserved_by.nickname,
            owner.nickname,
            owner.email,
            item.name,
            l.name,
            'Reserved',
            owner.nickname,
            owner.email,
        )
        body = self.ae.DELETED_ITEM_TEMPLATE % template_tuple
        msg = dict(f=self.ae.FROM_ADDRESS, t=o.email, s=subject, b=body)
        self.assertEqual(msg, self.ae.msg)

    def test_remove_purchased_item_from_own_list(self):
        '''
        Confirm ability to remove an item from your own list
        which has previously been marked as purchased, and that an
        email is sent to the purchaser
        '''
        self.create_lists(self.db.owner, 5)
        l = self.db.owner.lists[-1]
        count = len(l.items)
        item = l.items[0]
        iid = item.key().id()
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        item.purchased_by = o
        item.put()
        self.rpc.remove_item(iid)
        self.assertEqual(count - 1, len(self.db.owner.lists[-1].items))
        owner = self.db.owner
        subject = 'Wish List Item Deleted'
        template_tuple = (
            item.purchased_by.nickname,
            owner.nickname,
            owner.email,
            item.name,
            l.name,
            'Purchased',
            owner.nickname,
            owner.email,
        )
        body = self.ae.DELETED_ITEM_TEMPLATE % template_tuple
        msg = dict(f=self.ae.FROM_ADDRESS, t=o.email, s=subject, b=body)
        self.assertEqual(msg, self.ae.msg)

    def test_remove_item_from_invalid_list(self):
        '''
        Confirm that trying to remove an item from someone else's
        list raises PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        iid = o.lists[2].items[1].key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.remove_item(iid)

    def test_reserve_item(self):
        '''
        Confirm ability to reserve an item from list you can access
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        self.assertEqual(None, item.reserved_by)
        self.assertEqual(None, item.purchased_by)
        self.rpc.reserve_item(iid)
        self.assertEqual(self.db.owner, item.reserved_by)
        self.assertEqual(None, item.purchased_by)

    def test_reserve_reserved_item(self):
        '''
        Confirm trying to reserve a reserved item raises UserVisibleError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        o2 = self.db.add_owner(User('bar', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        item.reserved_by = o2
        item.put()
        with self.assertRaises(UserVisibleError):
            self.rpc.reserve_item(iid)

    def test_reserve_puchased_item(self):
        '''
        Confirm trying to reserve a purchased item raises UserVisibleError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        o2 = self.db.add_owner(User('bar', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        item.purchased_by = o2
        item.put()
        with self.assertRaises(UserVisibleError):
            self.rpc.reserve_item(iid)

    def test_unreserve_item(self):
        '''
        Confirm ability to unreserve and item previously reserved by you
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        item.reserved_by = self.db.owner
        item.put()
        iid = item.key().id()
        self.assertEqual(self.db.owner, item.reserved_by)
        self.assertEqual(None, item.purchased_by)
        self.rpc.unreserve_item(iid)
        self.assertEqual(None, item.reserved_by)
        self.assertEqual(None, item.purchased_by)

    def test_unreserve_item_not_reserved_by_you(self):
        '''
        Confirm that trying to unrserve an item reserved by someone else
        raises a UserVisibleError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        o2 = self.db.add_owner(User('bar', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        item.reserved_by = o2
        item.put()
        with self.assertRaises(UserVisibleError):
            self.rpc.unreserve_item(iid)

    def test_reserve_invalid_item(self):
        '''
        Confirm trying to reserve an item you don't have access to raises
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        item = o.lists[2].items[1]
        iid = item.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.reserve_item(iid)

    def test_unreserve_invalid_item(self):
        '''
        Confirm trying to unreserve an item you don't have access to raises
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        item = o.lists[2].items[1]
        iid = item.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.unreserve_item(iid)

    def test_purchase_item(self):
        '''
        Confirm ability to purchase an item from list you can access
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        self.assertEqual(None, item.reserved_by)
        self.assertEqual(None, item.purchased_by)
        self.rpc.purchase_item(iid)
        self.assertEqual(None, item.reserved_by)
        self.assertEqual(self.db.owner, item.purchased_by)

    def test_purchase_reserved_item(self):
        '''
        Confirm trying to purchase a reserved item raises UserVisibleError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        o2 = self.db.add_owner(User('bar', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        item.reserved_by = o2
        item.put()
        with self.assertRaises(UserVisibleError):
            self.rpc.purchase_item(iid)

    def test_purchase_puchased_item(self):
        '''
        Confirm trying to purchase a purchased item raises UserVisibleError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        o2 = self.db.add_owner(User('bar', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        item.purchased_by = o2
        item.put()
        with self.assertRaises(UserVisibleError):
            self.rpc.purchase_item(iid)

    def test_purchase_invalid_item(self):
        '''
        Confirm trying to purchase an item you don't have access to raises
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        item = o.lists[2].items[1]
        iid = item.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.purchase_item(iid)

    def test_unpurchase_invalid_item(self):
        '''
        Confirm trying to unpurchase an item you don't have access to raises
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        item = o.lists[2].items[1]
        iid = item.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.unpurchase_item(iid)

    def test_unpurchase_item(self):
        '''
        Confirm ability to unpurchase an item you previously purchased
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        item.purchased_by = self.db.owner
        item.put()
        iid = item.key().id()
        self.assertEqual(None, item.reserved_by)
        self.assertEqual(self.db.owner, item.purchased_by)
        self.rpc.unpurchase_item(iid)
        self.assertEqual(None, item.reserved_by)
        self.assertEqual(None, item.purchased_by)

    def test_unpurchase_item_purchased_by_someone_else(self):
        '''
        Confirm trying to unpurchase an item purchased by someone else
        raises a UserVisibleError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        o2 = self.db.add_owner(User('bar', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        item.purchased_by = o2
        item.put()
        with self.assertRaises(UserVisibleError):
            self.rpc.unpurchase_item(iid)
Exemplo n.º 4
0
 def setUp(self):
     self.db = MockAccess(User(), add_owner=True)
     self.ae = MockAppEngine()
     self.rpc = GroupRpcGroup(self.db, self.ae)
Exemplo n.º 5
0
class GroupRpcTest(unittest.TestCase):
    def setUp(self):
        self.db = MockAccess(User(), add_owner=True)
        self.ae = MockAppEngine()
        self.rpc = GroupRpcGroup(self.db, self.ae)

    def set_user(self, user):
        self.db.user = user

    def set_owner(self, owner):
        self.db.owner = owner
        self.db.user = owner.user

    def add_groups(self, count, include_invites=False):
        for i in xrange(1, count + 1):
            g = self.db.add_group('Name {}'.format(i), 'Desc {}'.format(i))
            self.assertEqual([], g.invitations)
            self.assertEqual([], g.members)
            if include_invites:
                self.db.add_group_invite(g, 'email_{}@domain.com'.format(i))

    def compare_group(self, db, group):
        self.assertEqual(db.key().id(), group.id)
        self.assertEqual(db.name, group.name)
        self.assertEqual(db.description, group.description)

    def setup_groups(self):
        O = self.db.add_owner
        self.a1 = O(User('a1', '*****@*****.**', is_admin=True))
        self.a2 = O(User('a2', '*****@*****.**', is_admin=True))
        self.u1 = O(User('u1', '*****@*****.**'))
        self.u2 = O(User('u2', '*****@*****.**'))
        self.u3 = O(User('u3', '*****@*****.**'))

        self.g1 = self.db.add_group('g1', 'g1desc', self.a1)
        self.g2 = self.db.add_group('g2', 'g1desc', self.a2)
        self.db.add_group_member(self.g1, self.a1)
        self.db.add_group_member(self.g1, self.u1)
        self.db.add_group_member(self.g1, self.u3)

        self.db.add_group_member(self.g2, self.a2)
        self.db.add_group_member(self.g2, self.u2)
        self.db.add_group_member(self.g2, self.u3)

    def Xtest_add_group(self):
        """
        Confirm that add_group actually adds a group
        """
        self.db.user.is_admin = True
        self.rpc.add_group('Group Name', 'Group Desc')
        self.assertEqual(1, len(self.db.groups))
        g = self.db.groups.values()[0]
        self.assertTrue(g.key().id() in range(1, 1001))
        self.assertEqual('Group Name', g.name)
        self.assertEqual('Group Desc', g.description)

    def Xtest_add_group_requires_admin(self):
        """
        Confirm trying to add group with non admin user raised Exception
        """
        with self.assertRaises(UserVisibleError):
            self.rpc.add_group('Name', 'Desc')

    def Xtest_add_group_duplicate_name(self):
        """
        Confirm that trying to add a group with a duplicate name raises
        a DuplicateNameError
        """
        self.db.user.is_admin = True
        self.add_groups(1)
        name = self.db.groups.values()[0].name
        with self.assertRaises(DuplicateNameError):
            self.rpc.add_group(name, 'Group Desc')

    def Xtest_get_groups(self):
        """
        Confirm group retrieval
        """
        self.add_groups(5)
        groups = self.rpc.get_groups()
        self.assertEqual(5, len(groups))
        for g, group in zip(self.db.get_groups(), groups):
            self.compare_group(g, group)

    def Xtest_update_group(self):
        """
        Confirm ability to change group name/desc
        """
        self.add_groups(1)
        id = self.db.group_ids[0]
        self.rpc.update_group(id, 'New Name', 'New Desc')
        g = self.db.get_group(id)
        self.assertEqual('New Name', g.name)
        self.assertEqual('New Desc', g.description)

    def Xtest_update_group_duplicate_name(self):
        """
        Confirm that trying to update a group with a used name raises a
        DuplicateNameError
        """
        self.add_groups(2)
        id = self.db.group_ids[0]
        name = self.db.get_group(self.db.group_ids[1]).name
        with self.assertRaises(DuplicateNameError):
            self.rpc.update_group(id, name, 'New Desc')

    def Xtest_update_group_same_name(self):
        """
        Confirm that trying to update a group with its own name and a new
        description does NOT raise a DuplicateNameError
        """
        self.add_groups(1)
        g = self.db.groups.values()[0]
        self.rpc.update_group(g.key().id(), g.name, 'New Desc')

    def Xtest_invite_member(self):
        """
        Confirm ability to invite someone to a group
        """
        self.add_groups(1)
        id = self.db.group_ids[0]
        group = self.db.groups[id]
        self.assertEqual(0, len(group.invitations))
        self.rpc.invite_member(id, '*****@*****.**')
        self.assertEqual(1, len(group.invitations))
        invite = group.invitations[0]
        self.assertEqual('*****@*****.**', invite.email)

    def Xtest_accept_invitation(self):
        """
        Confirm ability to accept a group invite
        """
        self.add_groups(1, include_invites=True)
        invite = self.db.invitations.values()[0]
        group = invite.group
        num_invites = len(group.invitations)
        self.rpc.accept_invitation(invite.key().id())
        self.assertEqual(num_invites - 1, len(group.invitations))
        self.assertEqual(1, len(group.members))
        self.assertEqual(self.db.owner, group.members[0].member)

    def Xtest_decline_invitation(self):
        """
        Confirm ability to decline a group invitation
        """
        self.add_groups(1, include_invites=True)
        invite = self.db.invitations.values()[0]
        group = invite.group
        num_invites = len(group.invitations)
        num_members = len(group.members)
        self.rpc.decline_invitation(invite.key().id())
        self.assertEqual(num_invites - 1, len(group.invitations))
        self.assertEqual(num_members, len(group.members))
        self.assertTrue(invite not in self.db.invitations.values())

    def test_get_available_owners_a1(self):
        """
        Confirm lookup of all available users for a1 owner
        """
        self.setup_groups()
        self.set_owner(self.a1)

        owners = self.rpc.get_available_owners(self.a1.key().id())
        self.assertEqual(3, len(owners))
        expected = set((o.email for o in (self.a1, self.u1, self.u3)))
        actual = set((o.email for o in owners))
        self.assertEqual(expected, actual)

    def test_get_available_owners_a2(self):
        """
        Confirm lookup of all available users for a2 owner
        """
        self.setup_groups()
        self.set_owner(self.a2)

        owners = self.rpc.get_available_owners(self.a2.key().id())
        self.assertEqual(3, len(owners))
        expected = set((o.email for o in (self.a2, self.u2, self.u3)))
        actual = set((o.email for o in owners))
        self.assertEqual(expected, actual)

    def test_get_available_owners_u1(self):
        """
        Confirm lookup of all available users for u1 owner
        """
        self.setup_groups()
        self.set_owner(self.u1)

        owners = self.rpc.get_available_owners(self.u1.key().id())
        self.assertEqual(3, len(owners))
        expected = set((o.email for o in (self.a1, self.u1, self.u3)))
        actual = set((o.email for o in owners))
        self.assertEqual(expected, actual)

    def test_get_available_owners_u2(self):
        """
        Confirm lookup of all available users for u2 owner
        """
        self.setup_groups()
        self.set_owner(self.u2)

        owners = self.rpc.get_available_owners(self.u2.key().id())
        self.assertEqual(3, len(owners))
        expected = set((o.email for o in (self.a2, self.u2, self.u3)))
        actual = set((o.email for o in owners))
        self.assertEqual(expected, actual)

    def test_get_available_owners_u3(self):
        """
        Confirm lookup of all available users for u3 owner
        """
        self.setup_groups()
        self.set_owner(self.u3)

        owners = self.rpc.get_available_owners(self.u3.key().id())
        self.assertEqual(5, len(owners))
        all = (self.a1, self.a2, self.u1, self.u2, self.u3)
        expected = set((o.email for o in all))
        actual = set((o.email for o in owners))
        self.assertEqual(expected, actual)
Exemplo n.º 6
0
 def set_user(self, user):
     self.user = user
     self.db = MockAccess(self.user)
     self.ae = MockAppEngine()
     self.rpc = UserRpcGroup(self.db, self.ae)
Exemplo n.º 7
0
class UserRpcTest(unittest.TestCase):

    def setUp(self):
        self.set_user(User())

    def set_user(self, user):
        self.user = user
        self.db = MockAccess(self.user)
        self.ae = MockAppEngine()
        self.rpc = UserRpcGroup(self.db, self.ae)

    def Xtest_get_user_none(self):
        """
        Verify that calling get_current_user without being logged in
        provides you with a login url
        """
        # Start with no user logged in
        self.set_user(None)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify nothing but login url is returned
        self.assertEqual(None, user.logout_url)
        self.assertEqual(None, user.email)
        self.assertEqual(None, user.user_id)
        self.assertEqual(None, user.nickname)
        login_url = self.ae.create_login_url(url)
        self.assertEqual(login_url, user.login_url)

    def Xtest_get_user(self):
        """
        Verify that calling get_current_user while logged in provides you
        with a logout url and your login info
        """

        self.set_user(User(is_admin=True))
        # Verify no owner exists before call
        self.assertEqual(None, self.db.user_owners.get(self.user, None))
        num_owners = len(self.db.owners)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify correct user details are returned
        base = get_base_url(url)
        goodbye = '{}/#Goodbye:'.format(base)
        self.assertEqual(self.ae.create_logout_url(goodbye), user.logout_url)
        self.assertEqual(self.user.nickname(), user.nickname)
        self.assertEqual(self.user.email(), user.email)
        self.assertEqual(None, user.login_url)

        # Verify owner was created correctly
        owner = self.db.user_owners.get(self.user)
        self.assertEqual(num_owners + 1, len(self.db.owners))
        self.assertEqual(self.user, owner.user)
        self.assertEqual(owner.key().id(), user.owner_id)
        self.assertEqual(self.user.nickname(), owner.nickname)
        self.assertEqual(self.user.email(), owner.email)
        self.assertEqual(extract_name(self.user.email()), owner.name)

    def Xtest_get_user_owner_exists(self):
        """
        Verify that a duplicate owner is not created when get_current_user
        is called
        """
        # Make sure owner exists before call
        owner = self.db.add_owner(self.user)
        num_owners = len(self.db.owners)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # verify still the same owner after the call
        self.assertEqual(num_owners, len(self.db.owners))
        self.assertEqual(owner.key().id(), user.owner_id)
        self.assertEqual(owner, self.db.user_owners[self.user])

    def Xtest_get_user_add_req(self):
        """
        Verify that when non-admin user tries to log in, an Access Request is
        created for them.
        """

        # Verify no owner exists before call
        self.assertEqual(None, self.db.user_owners.get(self.user, None))
        num_owners = len(self.db.owners)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify correct user details are returned
        base = get_base_url(url)
        goodbye = '{}/#Goodbye:'.format(base)
        self.assertEqual(self.ae.create_logout_url(goodbye), user.logout_url)
        self.assertEqual(self.user.nickname(), user.nickname)
        self.assertEqual(self.user.email(), user.email)
        self.assertEqual(None, user.login_url)
        self.assertEqual(-1, user.owner_id)
        self.assertEqual(num_owners, len(self.db.owners))

        # Verify access request was created correctly
        self.assertEqual(1, len(self.db.request_ids))
        req = self.db.requests.values()[0]
        self.assertEqual(self.user, req.user)

    def Xtest_get_user_req_exists(self):
        """
        Verify that the second time get_current_user is called, a second
        AccessRequest is not created.
        """

        # Verify no owner exists before call
        self.assertEqual(None, self.db.user_owners.get(self.user, None))
        num_owners = len(self.db.owners)

        # Create the request that already exists
        self.db.add_req(self.user)
        self.assertEqual(1, len(self.db.request_ids))

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify correct user details are returned
        self.assertEqual(None, user.login_url)
        self.assertEqual(-1, user.owner_id)
        self.assertEqual(num_owners, len(self.db.owners))

        # Verify no additional request was created correctly
        self.assertEqual(1, len(self.db.request_ids))

    def Xtest_get_owner(self):
        """
        Verify ability to lookup owner by id
        """
        # Make sure the owner exists
        owner = self.db.add_owner(self.user)
        oid = owner.key().id()
        lo = self.rpc.get_owner(oid)

        self.assertEqual(oid, lo.id)
        self.assertEqual(owner.email, lo.email)
        self.assertEqual(owner.nickname, lo.nickname)
        self.assertEqual(owner.name, lo.name)

    def test_update_owner(self):
        """
        Make sure user can change name and nickname of list owner
        """
        # Make sure the owner exists
        self.set_user(User('John Doe', 'Johnny'))
        self.db.add_owner(self.user)
        owner = self.db.add_owner(self.user)
        self.assertFalse(owner.saved())

        self.rpc.update_owner(owner.key().id(), 'Joe Smith', 'Joe')

        self.assertTrue(owner.saved())
        self.assertEqual('Joe Smith', owner.name)
        self.assertEqual('Joe', owner.nickname)
Exemplo n.º 8
0
class UserRpcTest(unittest.TestCase):

    def setUp(self):
        self.set_user(User())

    def set_user(self, user):
        self.user = user
        self.db = MockAccess(self.user)
        self.ae = MockWrapper()
        self.rpc = UserRpcGroup(self.db, self.ae)

    def test_get_user_none(self):
        '''
        Verify that calling get_current_user without being logged in
        provides you with a login url
        '''
        # Start with no user logged in
        self.set_user(None)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify nothing but login url is returned
        self.assertEqual(None, user.logout_url)
        self.assertEqual(None, user.email)
        self.assertEqual(None, user.user_id)
        self.assertEqual(None, user.nickname)
        login_url = self.ae.create_login_url(url)
        self.assertEqual(login_url, user.login_url)

    def test_get_user(self):
        '''
        Verify that calling get_current_user while logged in provides you
        with a logout url and your login info
        '''

        self.set_user(User(is_admin=True))
        # Verify no owner exists before call
        self.assertEqual(None, self.db.user_owners.get(self.user, None))
        num_owners = len(self.db.owners)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify correct user details are returned
        base = get_base_url(url)
        goodbye = '{}/#Goodbye:'.format(base)
        self.assertEqual(self.ae.create_logout_url(goodbye), user.logout_url)
        self.assertEqual(self.user.nickname(), user.nickname)
        self.assertEqual(self.user.email(), user.email)
        self.assertEqual(None, user.login_url)

        # Verify owner was created correctly
        owner = self.db.user_owners.get(self.user)
        self.assertEqual(num_owners + 1, len(self.db.owners))
        self.assertEqual(self.user, owner.user)
        self.assertEqual(owner.key().id(), user.owner_id)
        self.assertEqual(self.user.nickname(), owner.nickname)
        self.assertEqual(self.user.email(), owner.email)
        self.assertEqual(extract_name(self.user.email()), owner.name)

    def test_get_user_owner_exists(self):
        '''
        Verify that a duplicate owner is not created when get_current_user
        is called
        '''
        # Make sure owner exists before call
        owner = self.db.add_owner(self.user)
        num_owners = len(self.db.owners)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # verify still the same owner after the call
        self.assertEqual(num_owners, len(self.db.owners))
        self.assertEqual(owner.key().id(), user.owner_id)
        self.assertEqual(owner, self.db.user_owners[self.user])

    def test_get_user_add_req(self):
        '''
        Verify that when non-admin user tries to log in, an Access Request is
        created for them.
        '''

        # Verify no owner exists before call
        self.assertEqual(None, self.db.user_owners.get(self.user, None))
        num_owners = len(self.db.owners)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify correct user details are returned
        base = get_base_url(url)
        goodbye = '{}/#Goodbye:'.format(base)
        self.assertEqual(self.ae.create_logout_url(goodbye), user.logout_url)
        self.assertEqual(self.user.nickname(), user.nickname)
        self.assertEqual(self.user.email(), user.email)
        self.assertEqual(None, user.login_url)
        self.assertEqual(-1, user.owner_id)
        self.assertEqual(num_owners, len(self.db.owners))

        # Verify access request was created correctly
        self.assertEqual(1, len(self.db.request_ids))
        req = self.db.requests.values()[0]
        self.assertEqual(self.user, req.user)

    def test_get_user_req_exists(self):
        '''
        Verify that the second time get_current_user is called, a second
        AccessRequest is not created.
        '''

        # Verify no owner exists before call
        self.assertEqual(None, self.db.user_owners.get(self.user, None))
        num_owners = len(self.db.owners)

        # Create the request that already exists
        self.db.add_req(self.user)
        self.assertEqual(1, len(self.db.request_ids))

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify correct user details are returned
        self.assertEqual(None, user.login_url)
        self.assertEqual(-1, user.owner_id)
        self.assertEqual(num_owners, len(self.db.owners))

        # Verify no additional request was created correctly
        self.assertEqual(1, len(self.db.request_ids))

    def test_get_owner(self):
        '''
        Verify ability to lookup owner by id
        '''
        # Make sure the owner exists
        owner = self.db.add_owner(self.user)
        oid = owner.key().id()
        lo = self.rpc.get_owner(oid)

        self.assertEqual(oid, lo.id)
        self.assertEqual(owner.email, lo.email)
        self.assertEqual(owner.nickname, lo.nickname)
        self.assertEqual(owner.name, lo.name)

    def test_update_owner(self):
        '''
        Make sure user can change name and nickname of list owner
        '''
        # Make sure the owner exists
        self.set_user(User('John Doe', 'Johnny'))
        self.db.add_owner(self.user)
        owner = self.db.add_owner(self.user)
        self.assertFalse(owner.saved())

        self.rpc.update_owner(owner.key().id(), 'Joe Smith', 'Joe')

        self.assertTrue(owner.saved())
        self.assertEqual('Joe Smith', owner.name)
        self.assertEqual('Joe', owner.nickname)

    def test_approve_request_no_admin(self):
        '''
        Verify that attempting to approve a request as non admin user
        raises a PermissionDeniedError
        '''
        self.assertRaises(PermissionDeniedError, self.rpc.approve_request, 1)

    def test_deny_request_no_admin(self):
        '''
        Verify that attempting to approve a request as non admin user
        raises a PermissionDeniedError
        '''
        self.assertRaises(PermissionDeniedError, self.rpc.approve_request, 1)

    def test_approve_request(self):
        '''
        Confirm approving a request for access to all i want
        '''
        self.set_user(User(is_admin=True))
        user = User(email='*****@*****.**')
        req = self.db.add_req(user)
        self.assertEqual({}, self.ae.msg)

        self.rpc.approve_request(req.key().id())

        self.assertEqual(0, len(self.db.request_ids))
        owner = self.db.owners.values()[0]
        self.assertEqual(user, owner.user)
        msg = self.ae.msg
        self.assertEqual(self.ae.FROM_ADDRESS, msg['f'])
        self.assertEqual(owner.email, msg['t'])
        self.assertEqual('Account Activated', msg['s'])
        body = self.ae.APPROVE_TEMPLATE % extract_name(owner.email)
        self.assertEqual(body, msg['b'])

    def test_deny_request(self):
        '''
        Confirm verifying a request for access to all i want
        '''

        self.set_user(User(is_admin=True))
        user = User(email='*****@*****.**')
        req = self.db.add_req(user)
        self.assertEqual({}, self.ae.msg)

        self.rpc.approve_request(req.key().id())

        self.assertEqual(0, len(self.db.request_ids))
        owner = self.db.owners.values()[0]
        self.assertEqual(user, owner.user)
        msg = self.ae.msg
        self.assertEqual(self.ae.FROM_ADDRESS, msg['f'])
        self.assertEqual(owner.email, msg['t'])
        self.assertEqual('Account Activated', msg['s'])
        body = self.ae.APPROVE_TEMPLATE % extract_name(owner.email)
        self.assertEqual(body, msg['b'])
Exemplo n.º 9
0
class ListRpcTest(unittest.TestCase):

    def setUp(self):
        self.db = MockAccess(User(), add_owner=True)
        self.ae = MockWrapper()
        self.rpc = ListRpcGroup(self.db, self.ae)

    def set_user(self, user):
        self.db.user = user

    def set_owner(self, owner):
        self.db.owner = owner
        self.db.user = owner.user

    def delete_owner(self):
        oid = self.db.owner.key().id()
        self.db.delete(self.db.owner)
        self.db.owner = None
        return oid

    def create_group(self, created_by, members, name='Group', desc='Desc'):
        g = self.db.add_group(name, desc, created_by)
        [ self.db.add_group_member(g, o) for o in members ]
        return g

    def create_list(self, oid, idx, count):
        _ = lambda x: '{} {}'.format(x, idx)
        wl = self.db.add_list(oid, _('List'), _('List Desc'))
        lid = wl.key().id()
        for idx in xrange(count):
            self.db.add_list_item(lid, _('Item'), _('Cat'), _('Desc'),
                                  _('url'), idx % 2 == 0)

    def create_lists(self, owner, count):
        oid = owner.key().id()
        for i in xrange(count):
            self.create_list(oid, i, 2 * i)

    def compare_all_lists(self, lists, wlists):
        self.assertEqual(len(lists), len(wlists))
        for l, wl in zip(lists, wlists):
            self.compare_lists(l, wl)

    def compare_lists(self, l, wl):
        l_items, wl_items = (l.items, wl.items)
        self.assertEqual(len(l_items), len(wl_items))
        for item, witem in zip(l_items, wl_items):
            self.compare_items(item, witem)

    def compare_items(self, item, witem):
        ga, eq = (getattr, self.assertEqual)
        _ = lambda x: x.label() if x is not None else ''
        attrs = ('name', 'category', 'description', 'url', 'is_surprise')
        [ eq(ga(item, a), ga(witem, a)) for a in attrs ]
        eq(_(item.purchased_by), witem.purchased_by)
        eq(_(item.reserved_by), witem.reserved_by)

    def test_add_list(self):
        '''
        Confirm that add_list actually adds a list
        '''
        oid = self.db.owner.key().id()
        self.rpc.add_list(oid, 'List Name', 'List Desc')
        self.assertEqual(1, len(self.db.lists))
        l = self.db.lists.values()[0]
        self.assertTrue(l.key().id() in range(1, 1001))
        self.assertEqual('List Name', l.name)
        self.assertEqual('List Desc', l.description)

    def test_add_list_no_owner(self):
        '''
        Confirm that trying to add a list with no owner raises a
        PermissionDeniedError
        '''
        oid = self.delete_owner()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.add_list(oid, 'Name', 'Desc')

    def test_add_list_other_owner(self):
        '''
        Confirm that trying to add a list for another owner raises a
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        oid = o.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.add_list(oid, 'Name', 'Desc')

    def test_add_list_duplicate_name(self):
        '''
        Confirm that trying to add a list with a duplicate name raises
        a DuplicateNameError
        '''
        oid = self.db.owner.key().id()
        name = 'My Wish List'
        self.db.add_list(oid, name, 'Description')
        with self.assertRaises(DuplicateNameError):
            self.rpc.add_list(oid, name, 'Another List Description')

    def test_update_list(self):
        '''
        Confirm that update_list actually updates a list
        '''
        oid = self.db.owner.key().id()
        l = self.db.add_list(oid, 'Name', 'Description')
        lid = l.key().id()
        self.rpc.update_list(lid, 'New Name', 'New Desc')
        self.assertEqual(1, len(self.db.lists))
        l = self.db.lists.values()[0]
        self.assertEqual('New Name', l.name)
        self.assertEqual('New Desc', l.description)

    def test_update_list_no_owner(self):
        '''
        Confirm that trying to update a list with no owner raises a
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        l = self.db.add_list(o.key().id(), 'List Name', 'List Desc')
        lid = l.key().id()
        self.delete_owner()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.update_list(lid, 'Name', 'Desc')

    def test_update_list_invalid_owner(self):
        '''
        Confirm that trying to add a list for an invalid owner raises a
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        oid = o.key().id()
        l = self.db.add_list(oid, 'New List', 'New Desc')
        lid = l.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.update_list(lid, 'Name', 'Desc')

    def test_update_list_duplicate_name(self):
        '''
        Confirm that trying to update a list with a duplicate name raises
        a DuplicateNameError
        '''
        oid = self.db.owner.key().id()
        name = 'My Wish List'
        self.db.add_list(oid, name, 'Description')
        l = self.db.add_list(oid, 'Second Name', 'Second Description')
        lid = l.key().id()
        with self.assertRaises(DuplicateNameError):
            self.rpc.update_list(lid, name, 'Another List Description')

    def test_get_list_no_owner(self):
        '''
        Confirm that trying to get lists  with no owner raises a
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        oid = o.key().id()
        self.delete_owner()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.get_lists(oid)

    def test_get_lists_invalid_owner(self):
        '''
        Confirm that trying to get lists for an invalid owner raises
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        oid = o.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.get_lists(oid)

    def test_get_own_lists(self):
        '''
        Confirm that get_lists works for current owner
        '''
        oid = self.db.owner.key().id()

        def _add(i):
            return self.db.add_list(oid, 'List {}'.format(i),
                                    'Desc {}'.format(i))

        lists = [ _add(i) for i in '12345' ]
        wls = self.rpc.get_lists(oid)
        self.assertEqual(len(lists), len(wls))

    def test_get_lists_from_member_of_my_group(self):
        '''
        Confirm that get_lists works for an owner who is a member of
        a group owned by current owner
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(self.db.owner, [o])
        oid = o.key().id()
        wls = self.rpc.get_lists(oid)
        self.compare_all_lists(o.lists, wls)

    def test_get_lists_from_member_of_group_im_in(self):
        '''
        Confirm that get_lists works for an owner who is a member of
        a group current owner is also in
        '''
        o1 = self.db.add_owner(User('foo', '*****@*****.**'))
        o2 = self.db.add_owner(User('bar', '*****@*****.**'))
        self.create_lists(o1, 5)
        self.create_group(o2, [o1, self.db.owner])
        oid = o1.key().id()
        wls = self.rpc.get_lists(oid)
        self.compare_all_lists(o1.lists, wls)
        pass

    def test_get_lists_from_owner_of_group_im_in(self):
        '''
        Confirm that get_lists works for an owner who is the owner of
        a group current owner is in
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        oid = o.key().id()
        wls = self.rpc.get_lists(oid)
        self.compare_all_lists(o.lists, wls)

    def test_add_item_to_invalid_owner(self):
        '''
        Confirm that trying to add an item to the list of an invalid owner
        raises PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        lid = o.lists[0].key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.add_item(lid, 'item', 'cat', 'desc', 'url', False)

    def test_add_surprise_item_to_invalid_owner(self):
        '''
        Confirm that trying to add a surprise item to the list of an invalid
        owner raises PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        lid = o.lists[0].key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.add_item(lid, 'item', 'cat', 'desc', 'url', True)

    def test_add_surprise_item_for_fellow_group_memeber(self):
        '''
        Confirm ability to add a surprise item to a list you have access to
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        l = o.lists[2]
        count = len(l.items)
        lid = l.key().id()
        w_list = self.rpc.add_item(lid, 'item', 'cat', 'desc', 'url', True)
        witem = w_list.items[-1]
        self.assertEqual(count + 1, len(o.lists[2].items))
        self.assertEqual('item', witem.name)
        self.assertEqual('cat', witem.category)
        self.assertEqual('desc', witem.description)
        self.assertEqual('url', witem.url)
        self.assertEqual(True, witem.is_surprise)
        self.assertEqual(self.db.owner.nickname, witem.reserved_by)

    def test_add_item_to_own_list(self):
        '''
        Confirm ability to add an item to your own list
        '''
        self.create_lists(self.db.owner, 1)
        l = self.db.owner.lists[0]
        count = len(l.items)
        lid = l.key().id()
        w_list = self.rpc.add_item(lid, 'item', 'cat', 'desc', 'url', False)
        witem = w_list.items[0]
        self.assertEqual(count + 1, len(self.db.owner.lists[0].items))
        self.assertEqual('item', witem.name)
        self.assertEqual('cat', witem.category)
        self.assertEqual('desc', witem.description)
        self.assertEqual('url', witem.url)
        self.assertEqual(False, witem.is_surprise)
        self.assertEqual('', witem.reserved_by)
        self.assertEqual('', witem.purchased_by)

    def test_update_item_in_own_list(self):
        '''
        Confirm ability to update an existing item in your own list
        '''
        self.create_lists(self.db.owner, 2)
        l = self.db.owner.lists[1]
        count = len(l.items)
        item = l.items[0]
        iid = item.key().id()
        n, c, d, u = ('New Name', 'New Cat', 'New Desc', 'New URL')
        w_list = self.rpc.update_item(iid, n, c, d, u)
        witem = w_list.items[0]
        self.assertEqual(count, len(self.db.owner.lists[1].items))
        self.assertEqual(n, witem.name)
        self.assertEqual(c, witem.category)
        self.assertEqual(d, witem.description)
        self.assertEqual(u, witem.url)

    def test_update_item_in_invalid_list(self):
        ''''
        Confirm that trying to update an item in someone else's list
        raises PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[2].items[1]
        iid = item.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.update_item(iid, 'item', 'cat', 'desc', 'url')

    def test_remove_item_from_own_list(self):
        '''
        Confirm ability to remove an item from your own list
        '''
        self.create_lists(self.db.owner, 5)
        l = self.db.owner.lists[3]
        count = len(l.items)
        item = l.items[0]
        iid = item.key().id()
        self.rpc.remove_item(iid)
        self.assertEqual(count - 1, len(self.db.owner.lists[3].items))
        self.assertTrue(item not in self.db.owner.lists[3].items)

    def test_remove_reserved_item_from_own_list(self):
        '''
        Confirm ability to remove an item from your own list
        which has previously been marked as reserved, and that an
        email is sent to the reserver
        '''
        self.create_lists(self.db.owner, 5)
        l = self.db.owner.lists[-1]
        count = len(l.items)
        item = l.items[0]
        iid = item.key().id()
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        item.reserved_by = o
        item.put()
        self.rpc.remove_item(iid)
        self.assertEqual(count - 1, len(self.db.owner.lists[-1].items))
        owner = self.db.owner
        subject = 'Wish List Item Deleted'
        template_tuple = (
            item.reserved_by.nickname,
            owner.nickname, owner.email, item.name, l.name, 'Reserved',
            owner.nickname, owner.email,
        )
        body = self.ae.DELETED_ITEM_TEMPLATE % template_tuple
        msg = dict(f=self.ae.FROM_ADDRESS, t=o.email, s=subject, b=body)
        self.assertEqual(msg, self.ae.msg)

    def test_remove_purchased_item_from_own_list(self):
        '''
        Confirm ability to remove an item from your own list
        which has previously been marked as purchased, and that an
        email is sent to the purchaser
        '''
        self.create_lists(self.db.owner, 5)
        l = self.db.owner.lists[-1]
        count = len(l.items)
        item = l.items[0]
        iid = item.key().id()
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        item.purchased_by = o
        item.put()
        self.rpc.remove_item(iid)
        self.assertEqual(count - 1, len(self.db.owner.lists[-1].items))
        owner = self.db.owner
        subject = 'Wish List Item Deleted'
        template_tuple = (
            item.purchased_by.nickname,
            owner.nickname, owner.email, item.name, l.name, 'Purchased',
            owner.nickname, owner.email,
        )
        body = self.ae.DELETED_ITEM_TEMPLATE % template_tuple
        msg = dict(f=self.ae.FROM_ADDRESS, t=o.email, s=subject, b=body)
        self.assertEqual(msg, self.ae.msg)

    def test_remove_item_from_invalid_list(self):
        '''
        Confirm that trying to remove an item from someone else's
        list raises PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        iid = o.lists[2].items[1].key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.remove_item(iid)

    def test_reserve_item(self):
        '''
        Confirm ability to reserve an item from list you can access
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        self.assertEqual(None, item.reserved_by)
        self.assertEqual(None, item.purchased_by)
        self.rpc.reserve_item(iid)
        self.assertEqual(self.db.owner, item.reserved_by)
        self.assertEqual(None, item.purchased_by)

    def test_reserve_reserved_item(self):
        '''
        Confirm trying to reserve a reserved item raises UserVisibleError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        o2 = self.db.add_owner(User('bar', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        item.reserved_by = o2
        item.put()
        with self.assertRaises(UserVisibleError):
            self.rpc.reserve_item(iid)

    def test_reserve_puchased_item(self):
        '''
        Confirm trying to reserve a purchased item raises UserVisibleError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        o2 = self.db.add_owner(User('bar', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        item.purchased_by = o2
        item.put()
        with self.assertRaises(UserVisibleError):
            self.rpc.reserve_item(iid)

    def test_unreserve_item(self):
        '''
        Confirm ability to unreserve and item previously reserved by you
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        item.reserved_by = self.db.owner
        item.put()
        iid = item.key().id()
        self.assertEqual(self.db.owner, item.reserved_by)
        self.assertEqual(None, item.purchased_by)
        self.rpc.unreserve_item(iid)
        self.assertEqual(None, item.reserved_by)
        self.assertEqual(None, item.purchased_by)

    def test_unreserve_item_not_reserved_by_you(self):
        '''
        Confirm that trying to unrserve an item reserved by someone else
        raises a UserVisibleError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        o2 = self.db.add_owner(User('bar', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        item.reserved_by = o2
        item.put()
        with self.assertRaises(UserVisibleError):
            self.rpc.unreserve_item(iid)

    def test_reserve_invalid_item(self):
        '''
        Confirm trying to reserve an item you don't have access to raises
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        item = o.lists[2].items[1]
        iid = item.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.reserve_item(iid)

    def test_unreserve_invalid_item(self):
        '''
        Confirm trying to unreserve an item you don't have access to raises
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        item = o.lists[2].items[1]
        iid = item.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.unreserve_item(iid)

    def test_purchase_item(self):
        '''
        Confirm ability to purchase an item from list you can access
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        self.assertEqual(None, item.reserved_by)
        self.assertEqual(None, item.purchased_by)
        self.rpc.purchase_item(iid)
        self.assertEqual(None, item.reserved_by)
        self.assertEqual(self.db.owner, item.purchased_by)

    def test_purchase_reserved_item(self):
        '''
        Confirm trying to purchase a reserved item raises UserVisibleError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        o2 = self.db.add_owner(User('bar', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        item.reserved_by = o2
        item.put()
        with self.assertRaises(UserVisibleError):
            self.rpc.purchase_item(iid)

    def test_purchase_puchased_item(self):
        '''
        Confirm trying to purchase a purchased item raises UserVisibleError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        o2 = self.db.add_owner(User('bar', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        item.purchased_by = o2
        item.put()
        with self.assertRaises(UserVisibleError):
            self.rpc.purchase_item(iid)

    def test_purchase_invalid_item(self):
        '''
        Confirm trying to purchase an item you don't have access to raises
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        item = o.lists[2].items[1]
        iid = item.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.purchase_item(iid)

    def test_unpurchase_invalid_item(self):
        '''
        Confirm trying to unpurchase an item you don't have access to raises
        PermissionDeniedError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        item = o.lists[2].items[1]
        iid = item.key().id()
        with self.assertRaises(PermissionDeniedError):
            self.rpc.unpurchase_item(iid)

    def test_unpurchase_item(self):
        '''
        Confirm ability to unpurchase an item you previously purchased
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        item.purchased_by = self.db.owner
        item.put()
        iid = item.key().id()
        self.assertEqual(None, item.reserved_by)
        self.assertEqual(self.db.owner, item.purchased_by)
        self.rpc.unpurchase_item(iid)
        self.assertEqual(None, item.reserved_by)
        self.assertEqual(None, item.purchased_by)

    def test_unpurchase_item_purchased_by_someone_else(self):
        '''
        Confirm trying to unpurchase an item purchased by someone else
        raises a UserVisibleError
        '''
        o = self.db.add_owner(User('foo', '*****@*****.**'))
        o2 = self.db.add_owner(User('bar', '*****@*****.**'))
        self.create_lists(o, 5)
        self.create_group(o, [self.db.owner])
        item = o.lists[-1].items[-1]
        iid = item.key().id()
        item.purchased_by = o2
        item.put()
        with self.assertRaises(UserVisibleError):
            self.rpc.unpurchase_item(iid)
Exemplo n.º 10
0
 def setUp(self):
     self.db = MockAccess(User(), add_owner=True)
     self.ae = MockWrapper()
     self.rpc = ListRpcGroup(self.db, self.ae)
Exemplo n.º 11
0
 def set_user(self, user):
     self.user = user
     self.db = MockAccess(self.user)
     self.ae = MockAppEngine()
     self.rpc = UserRpcGroup(self.db, self.ae)
Exemplo n.º 12
0
class UserRpcTest(unittest.TestCase):
    def setUp(self):
        self.set_user(User())

    def set_user(self, user):
        self.user = user
        self.db = MockAccess(self.user)
        self.ae = MockAppEngine()
        self.rpc = UserRpcGroup(self.db, self.ae)

    def Xtest_get_user_none(self):
        """
        Verify that calling get_current_user without being logged in
        provides you with a login url
        """
        # Start with no user logged in
        self.set_user(None)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify nothing but login url is returned
        self.assertEqual(None, user.logout_url)
        self.assertEqual(None, user.email)
        self.assertEqual(None, user.user_id)
        self.assertEqual(None, user.nickname)
        login_url = self.ae.create_login_url(url)
        self.assertEqual(login_url, user.login_url)

    def Xtest_get_user(self):
        """
        Verify that calling get_current_user while logged in provides you
        with a logout url and your login info
        """

        self.set_user(User(is_admin=True))
        # Verify no owner exists before call
        self.assertEqual(None, self.db.user_owners.get(self.user, None))
        num_owners = len(self.db.owners)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify correct user details are returned
        base = get_base_url(url)
        goodbye = '{}/#Goodbye:'.format(base)
        self.assertEqual(self.ae.create_logout_url(goodbye), user.logout_url)
        self.assertEqual(self.user.nickname(), user.nickname)
        self.assertEqual(self.user.email(), user.email)
        self.assertEqual(None, user.login_url)

        # Verify owner was created correctly
        owner = self.db.user_owners.get(self.user)
        self.assertEqual(num_owners + 1, len(self.db.owners))
        self.assertEqual(self.user, owner.user)
        self.assertEqual(owner.key().id(), user.owner_id)
        self.assertEqual(self.user.nickname(), owner.nickname)
        self.assertEqual(self.user.email(), owner.email)
        self.assertEqual(extract_name(self.user.email()), owner.name)

    def Xtest_get_user_owner_exists(self):
        """
        Verify that a duplicate owner is not created when get_current_user
        is called
        """
        # Make sure owner exists before call
        owner = self.db.add_owner(self.user)
        num_owners = len(self.db.owners)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # verify still the same owner after the call
        self.assertEqual(num_owners, len(self.db.owners))
        self.assertEqual(owner.key().id(), user.owner_id)
        self.assertEqual(owner, self.db.user_owners[self.user])

    def Xtest_get_user_add_req(self):
        """
        Verify that when non-admin user tries to log in, an Access Request is
        created for them.
        """

        # Verify no owner exists before call
        self.assertEqual(None, self.db.user_owners.get(self.user, None))
        num_owners = len(self.db.owners)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify correct user details are returned
        base = get_base_url(url)
        goodbye = '{}/#Goodbye:'.format(base)
        self.assertEqual(self.ae.create_logout_url(goodbye), user.logout_url)
        self.assertEqual(self.user.nickname(), user.nickname)
        self.assertEqual(self.user.email(), user.email)
        self.assertEqual(None, user.login_url)
        self.assertEqual(-1, user.owner_id)
        self.assertEqual(num_owners, len(self.db.owners))

        # Verify access request was created correctly
        self.assertEqual(1, len(self.db.request_ids))
        req = self.db.requests.values()[0]
        self.assertEqual(self.user, req.user)

    def Xtest_get_user_req_exists(self):
        """
        Verify that the second time get_current_user is called, a second
        AccessRequest is not created.
        """

        # Verify no owner exists before call
        self.assertEqual(None, self.db.user_owners.get(self.user, None))
        num_owners = len(self.db.owners)

        # Create the request that already exists
        self.db.add_req(self.user)
        self.assertEqual(1, len(self.db.request_ids))

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify correct user details are returned
        self.assertEqual(None, user.login_url)
        self.assertEqual(-1, user.owner_id)
        self.assertEqual(num_owners, len(self.db.owners))

        # Verify no additional request was created correctly
        self.assertEqual(1, len(self.db.request_ids))

    def Xtest_get_owner(self):
        """
        Verify ability to lookup owner by id
        """
        # Make sure the owner exists
        owner = self.db.add_owner(self.user)
        oid = owner.key().id()
        lo = self.rpc.get_owner(oid)

        self.assertEqual(oid, lo.id)
        self.assertEqual(owner.email, lo.email)
        self.assertEqual(owner.nickname, lo.nickname)
        self.assertEqual(owner.name, lo.name)

    def test_update_owner(self):
        """
        Make sure user can change name and nickname of list owner
        """
        # Make sure the owner exists
        self.set_user(User('John Doe', 'Johnny'))
        self.db.add_owner(self.user)
        owner = self.db.add_owner(self.user)
        self.assertFalse(owner.saved())

        self.rpc.update_owner(owner.key().id(), 'Joe Smith', 'Joe')

        self.assertTrue(owner.saved())
        self.assertEqual('Joe Smith', owner.name)
        self.assertEqual('Joe', owner.nickname)
Exemplo n.º 13
0
class GroupRpcTest(unittest.TestCase):

    def setUp(self):
        self.db = MockAccess(User(), add_owner=True)
        self.ae = MockWrapper()
        self.rpc = GroupRpcGroup(self.db, self.ae)

    def set_user(self, user):
        self.db.user = user

    def set_owner(self, owner):
        self.db.owner = owner
        self.db.user = owner.user

    def add_groups(self, count, include_invites=False):
        for i in xrange(1, count + 1):
            g = self.db.add_group('Name {}'.format(i), 'Desc {}'.format(i))
            self.assertEqual([], g.invitations)
            self.assertEqual([], g.members)
            if include_invites:
                self.db.add_group_invite(g, 'email_{}@domain.com'.format(i))

    def compare_group(self, db, group):
        self.assertEqual(db.key().id(), group.id)
        self.assertEqual(db.name, group.name)
        self.assertEqual(db.description, group.description)

    def setup_groups(self):
        O = self.db.add_owner
        self.a1 = O(User('a1', '*****@*****.**', is_admin=True))
        self.a2 = O(User('a2', '*****@*****.**', is_admin=True))
        self.u1 = O(User('u1', '*****@*****.**'))
        self.u2 = O(User('u2', '*****@*****.**'))
        self.u3 = O(User('u3', '*****@*****.**'))

        self.g1 = self.db.add_group('g1', 'g1desc', self.a1)
        self.g2 = self.db.add_group('g2', 'g1desc', self.a2)
        self.db.add_group_member(self.g1, self.a1)
        self.db.add_group_member(self.g1, self.u1)
        self.db.add_group_member(self.g1, self.u3)

        self.db.add_group_member(self.g2, self.a2)
        self.db.add_group_member(self.g2, self.u2)
        self.db.add_group_member(self.g2, self.u3)

    def test_add_group(self):
        '''
        Confirm that add_group actually adds a group
        '''
        self.db.user.is_admin = True
        self.rpc.add_group('Group Name', 'Group Desc')
        self.assertEqual(1, len(self.db.groups))
        g = self.db.groups.values()[0]
        self.assertTrue(g.key().id() in range(1, 1001))
        self.assertEqual('Group Name', g.name)
        self.assertEqual('Group Desc', g.description)

    def test_add_group_requires_admin(self):
        '''
        Confirm trying to add group with non admin user raised Exception
        '''
        with self.assertRaises(UserVisibleError):
            self.rpc.add_group('Name', 'Desc')

    def test_add_group_duplicate_name(self):
        '''
        Confirm that trying to add a group with a duplicate name raises
        a DuplicateNameError
        '''
        self.db.user.is_admin = True
        self.add_groups(1)
        name = self.db.groups.values()[0].name
        with self.assertRaises(DuplicateNameError):
            self.rpc.add_group(name, 'Group Desc')

    def test_get_groups(self):
        '''
        Confirm group retrieval
        '''
        self.add_groups(5)
        groups = self.rpc.get_groups()
        self.assertEqual(5, len(groups))
        for g, group in zip(self.db.get_groups(), groups):
            self.compare_group(g, group)

    def test_update_group(self):
        '''
        Confirm ability to change group name/desc
        '''
        self.add_groups(1)
        id = self.db.group_ids[0]
        self.rpc.update_group(id, 'New Name', 'New Desc')
        g = self.db.get_group(id)
        self.assertEqual('New Name', g.name)
        self.assertEqual('New Desc', g.description)

    def test_update_group_duplicate_name(self):
        '''
        Confirm that trying to update a group with a used name raises a
        DuplicateNameError
        '''
        self.add_groups(2)
        id = self.db.group_ids[0]
        name = self.db.get_group(self.db.group_ids[1]).name
        with self.assertRaises(DuplicateNameError):
            self.rpc.update_group(id, name, 'New Desc')

    def test_update_group_same_name(self):
        '''
        Confirm that trying to update a group with its own name and a new
        description does NOT raise a DuplicateNameError
        '''
        self.add_groups(1)
        g = self.db.groups.values()[0]
        self.rpc.update_group(g.key().id(), g.name, 'New Desc')

    def test_invite_member(self):
        '''
        Confirm ability to invite someone to a group
        '''
        self.add_groups(1)
        id = self.db.group_ids[0]
        group = self.db.groups[id]
        self.assertEqual(0, len(group.invitations))
        self.rpc.invite_member(id, '*****@*****.**')
        self.assertEqual(1, len(group.invitations))
        invite = group.invitations[0]
        self.assertEqual('*****@*****.**', invite.email)

    def test_accept_invitation(self):
        '''
        Confirm ability to accept a group invite
        '''
        self.add_groups(1, include_invites=True)
        invite = self.db.invitations.values()[0]
        group = invite.group
        num_invites = len(group.invitations)
        self.rpc.accept_invitation(invite.key().id())
        self.assertEqual(num_invites - 1, len(group.invitations))
        self.assertEqual(1, len(group.members))
        self.assertEqual(self.db.owner, group.members[0].member)

    def test_decline_invitation(self):
        '''
        Confirm ability to decline a group invitation
        '''
        self.add_groups(1, include_invites=True)
        invite = self.db.invitations.values()[0]
        group = invite.group
        num_invites = len(group.invitations)
        num_members = len(group.members)
        self.rpc.decline_invitation(invite.key().id())
        self.assertEqual(num_invites - 1, len(group.invitations))
        self.assertEqual(num_members, len(group.members))
        self.assertTrue(invite not in self.db.invitations.values())

    def test_get_available_owners_a1(self):
        '''
        Confirm lookup of all available users for a1 owner
        '''
        self.setup_groups()
        self.set_owner(self.a1)

        owners = self.rpc.get_available_owners(self.a1.key().id())
        self.assertEqual(3, len(owners))
        expected = set((o.email for o in (self.a1, self.u1, self.u3)))
        actual = set((o.email for o in owners))
        self.assertEqual(expected, actual)

    def test_get_available_owners_a2(self):
        '''
        Confirm lookup of all available users for a2 owner
        '''
        self.setup_groups()
        self.set_owner(self.a2)

        owners = self.rpc.get_available_owners(self.a2.key().id())
        self.assertEqual(3, len(owners))
        expected = set((o.email for o in (self.a2, self.u2, self.u3)))
        actual = set((o.email for o in owners))
        self.assertEqual(expected, actual)

    def test_get_available_owners_u1(self):
        '''
        Confirm lookup of all available users for u1 owner
        '''
        self.setup_groups()
        self.set_owner(self.u1)

        owners = self.rpc.get_available_owners(self.u1.key().id())
        self.assertEqual(3, len(owners))
        expected = set((o.email for o in (self.a1, self.u1, self.u3)))
        actual = set((o.email for o in owners))
        self.assertEqual(expected, actual)

    def test_get_available_owners_u2(self):
        '''
        Confirm lookup of all available users for u2 owner
        '''
        self.setup_groups()
        self.set_owner(self.u2)

        owners = self.rpc.get_available_owners(self.u2.key().id())
        self.assertEqual(3, len(owners))
        expected = set((o.email for o in (self.a2, self.u2, self.u3)))
        actual = set((o.email for o in owners))
        self.assertEqual(expected, actual)

    def test_get_available_owners_u3(self):
        '''
        Confirm lookup of all available users for u3 owner
        '''
        self.setup_groups()
        self.set_owner(self.u3)

        owners = self.rpc.get_available_owners(self.u3.key().id())
        self.assertEqual(5, len(owners))
        all = (self.a1, self.a2, self.u1, self.u2, self.u3)
        expected = set((o.email for o in all))
        actual = set((o.email for o in owners))
        self.assertEqual(expected, actual)
Exemplo n.º 14
0
class UserRpcTest(unittest.TestCase):
    def setUp(self):
        self.set_user(User())

    def set_user(self, user):
        self.user = user
        self.db = MockAccess(self.user)
        self.ae = MockWrapper()
        self.rpc = UserRpcGroup(self.db, self.ae)

    def test_get_user_none(self):
        '''
        Verify that calling get_current_user without being logged in
        provides you with a login url
        '''
        # Start with no user logged in
        self.set_user(None)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify nothing but login url is returned
        self.assertEqual(None, user.logout_url)
        self.assertEqual(None, user.email)
        self.assertEqual(None, user.user_id)
        self.assertEqual(None, user.nickname)
        login_url = self.ae.create_login_url(url)
        self.assertEqual(login_url, user.login_url)

    def test_get_user(self):
        '''
        Verify that calling get_current_user while logged in provides you
        with a logout url and your login info
        '''

        self.set_user(User(is_admin=True))
        # Verify no owner exists before call
        self.assertEqual(None, self.db.user_owners.get(self.user, None))
        num_owners = len(self.db.owners)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify correct user details are returned
        base = get_base_url(url)
        goodbye = '{}/#Goodbye:'.format(base)
        self.assertEqual(self.ae.create_logout_url(goodbye), user.logout_url)
        self.assertEqual(self.user.nickname(), user.nickname)
        self.assertEqual(self.user.email(), user.email)
        self.assertEqual(None, user.login_url)

        # Verify owner was created correctly
        owner = self.db.user_owners.get(self.user)
        self.assertEqual(num_owners + 1, len(self.db.owners))
        self.assertEqual(self.user, owner.user)
        self.assertEqual(owner.key().id(), user.owner_id)
        self.assertEqual(self.user.nickname(), owner.nickname)
        self.assertEqual(self.user.email(), owner.email)
        self.assertEqual(extract_name(self.user.email()), owner.name)

    def test_get_user_owner_exists(self):
        '''
        Verify that a duplicate owner is not created when get_current_user
        is called
        '''
        # Make sure owner exists before call
        owner = self.db.add_owner(self.user)
        num_owners = len(self.db.owners)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # verify still the same owner after the call
        self.assertEqual(num_owners, len(self.db.owners))
        self.assertEqual(owner.key().id(), user.owner_id)
        self.assertEqual(owner, self.db.user_owners[self.user])

    def test_get_user_add_req(self):
        '''
        Verify that when non-admin user tries to log in, an Access Request is
        created for them.
        '''

        # Verify no owner exists before call
        self.assertEqual(None, self.db.user_owners.get(self.user, None))
        num_owners = len(self.db.owners)

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify correct user details are returned
        base = get_base_url(url)
        goodbye = '{}/#Goodbye:'.format(base)
        self.assertEqual(self.ae.create_logout_url(goodbye), user.logout_url)
        self.assertEqual(self.user.nickname(), user.nickname)
        self.assertEqual(self.user.email(), user.email)
        self.assertEqual(None, user.login_url)
        self.assertEqual(-1, user.owner_id)
        self.assertEqual(num_owners, len(self.db.owners))

        # Verify access request was created correctly
        self.assertEqual(1, len(self.db.request_ids))
        req = self.db.requests.values()[0]
        self.assertEqual(self.user, req.user)

    def test_get_user_req_exists(self):
        '''
        Verify that the second time get_current_user is called, a second
        AccessRequest is not created.
        '''

        # Verify no owner exists before call
        self.assertEqual(None, self.db.user_owners.get(self.user, None))
        num_owners = len(self.db.owners)

        # Create the request that already exists
        self.db.add_req(self.user)
        self.assertEqual(1, len(self.db.request_ids))

        url = 'http://www.domain.com/some/extra/path/'
        user = self.rpc.get_current_user(url)

        # Verify correct user details are returned
        self.assertEqual(None, user.login_url)
        self.assertEqual(-1, user.owner_id)
        self.assertEqual(num_owners, len(self.db.owners))

        # Verify no additional request was created correctly
        self.assertEqual(1, len(self.db.request_ids))

    def test_get_owner(self):
        '''
        Verify ability to lookup owner by id
        '''
        # Make sure the owner exists
        owner = self.db.add_owner(self.user)
        oid = owner.key().id()
        lo = self.rpc.get_owner(oid)

        self.assertEqual(oid, lo.id)
        self.assertEqual(owner.email, lo.email)
        self.assertEqual(owner.nickname, lo.nickname)
        self.assertEqual(owner.name, lo.name)

    def test_update_owner(self):
        '''
        Make sure user can change name and nickname of list owner
        '''
        # Make sure the owner exists
        self.set_user(User('John Doe', 'Johnny'))
        self.db.add_owner(self.user)
        owner = self.db.add_owner(self.user)
        self.assertFalse(owner.saved())

        self.rpc.update_owner(owner.key().id(), 'Joe Smith', 'Joe')

        self.assertTrue(owner.saved())
        self.assertEqual('Joe Smith', owner.name)
        self.assertEqual('Joe', owner.nickname)

    def test_approve_request_no_admin(self):
        '''
        Verify that attempting to approve a request as non admin user
        raises a PermissionDeniedError
        '''
        self.assertRaises(PermissionDeniedError, self.rpc.approve_request, 1)

    def test_deny_request_no_admin(self):
        '''
        Verify that attempting to approve a request as non admin user
        raises a PermissionDeniedError
        '''
        self.assertRaises(PermissionDeniedError, self.rpc.approve_request, 1)

    def test_approve_request(self):
        '''
        Confirm approving a request for access to all i want
        '''
        self.set_user(User(is_admin=True))
        user = User(email='*****@*****.**')
        req = self.db.add_req(user)
        self.assertEqual({}, self.ae.msg)

        self.rpc.approve_request(req.key().id())

        self.assertEqual(0, len(self.db.request_ids))
        owner = self.db.owners.values()[0]
        self.assertEqual(user, owner.user)
        msg = self.ae.msg
        self.assertEqual(self.ae.FROM_ADDRESS, msg['f'])
        self.assertEqual(owner.email, msg['t'])
        self.assertEqual('Account Activated', msg['s'])
        body = self.ae.APPROVE_TEMPLATE % extract_name(owner.email)
        self.assertEqual(body, msg['b'])

    def test_deny_request(self):
        '''
        Confirm verifying a request for access to all i want
        '''

        self.set_user(User(is_admin=True))
        user = User(email='*****@*****.**')
        req = self.db.add_req(user)
        self.assertEqual({}, self.ae.msg)

        self.rpc.approve_request(req.key().id())

        self.assertEqual(0, len(self.db.request_ids))
        owner = self.db.owners.values()[0]
        self.assertEqual(user, owner.user)
        msg = self.ae.msg
        self.assertEqual(self.ae.FROM_ADDRESS, msg['f'])
        self.assertEqual(owner.email, msg['t'])
        self.assertEqual('Account Activated', msg['s'])
        body = self.ae.APPROVE_TEMPLATE % extract_name(owner.email)
        self.assertEqual(body, msg['b'])
Exemplo n.º 15
0
 def setUp(self):
     self.db = MockAccess(User(), add_owner=True)
     self.ae = MockAppEngine()
     self.rpc = GroupRpcGroup(self.db, self.ae)