Example #1
0
    def add_users(self):

        admin_url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST,
                                      SYNC_GATEWAY_ADMIN_PORT)

        couchbase_utils.add_person_to_gateway(admin_url,
                                              SYNC_GATEWAY_NAME,
                                              "paul_id",
                                              "paul",
                                              "password1",
                                              admin_channels=["cars", "paul"])

        couchbase_utils.add_person_to_gateway(admin_url,
                                              SYNC_GATEWAY_NAME,
                                              "sol_id",
                                              "sol",
                                              "password2",
                                              admin_channels=["sol"])

        paul_url = "http://*****:*****@%s:%s" % (SYNC_GATEWAY_HOST,
                                                    SYNC_GATEWAY_PORT)
        self.paul_db = SyncGatewayWrapper(self.mapper, paul_url,
                                          SYNC_GATEWAY_NAME)

        sol_url = "http://*****:*****@%s:%s" % (SYNC_GATEWAY_HOST,
                                                  SYNC_GATEWAY_PORT)
        self.sol_db = SyncGatewayWrapper(self.mapper, sol_url,
                                         SYNC_GATEWAY_NAME)
Example #2
0
        def start_gateway(self):

            cmd = "{} -log=* -url walrus: ".format(SYNC_GATEWAY_PATH)
            # print cmd

            time.sleep(0.25)
            self.gateway = subprocess.Popen(cmd, shell=True)
            time.sleep(0.25)

            admin_url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST, SYNC_GATEWAY_ADMIN_PORT)
            self.admin_db = SyncGatewayWrapper(self.mapper, admin_url, SYNC_GATEWAY_NAME)
            self.admin_db.update_designs()
            self.add_users()
Example #3
0
    def setUp(self):
        cmd = "{} -log=* -url walrus: ".format(SYNC_GATEWAY_PATH)
        # print cmd

        time.sleep(0.25)
        self.gateway = subprocess.Popen(cmd, shell=True)
        time.sleep(0.25)
        filepath = os.path.join(DATA_PATH, "animal_views.js")
        mapper = ClassMapper(
            [Dog, Cat, Person, JackRussell, Monkey, Monarch, Monster],
            designs=[filepath])

        url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST,
                                SYNC_GATEWAY_ADMIN_PORT)
        self.db = SyncGatewayWrapper(mapper, url, SYNC_GATEWAY_NAME)
        self.db.update_designs()
        super(self.__class__, self).setUp()
Example #4
0
        def start_gateway(self, conf_file_path):

            cmd = "{} -log=* {}".format(SYNC_GATEWAY_PATH, conf_file_path)
            print cmd

            time.sleep(0.25)
            self.gateway = subprocess.Popen(cmd, shell=True)
            time.sleep(0.25)

            admin_url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST, SYNC_GATEWAY_ADMIN_PORT)
            self.admin_db = SyncGatewayWrapper(self.mapper, admin_url, SYNC_GATEWAY_NAME)
            self.admin_db.update_designs()
            self.add_users()
Example #5
0
        def add_users(self):

            admin_url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST, SYNC_GATEWAY_ADMIN_PORT)

            couchbase_utils.add_person_to_gateway(admin_url,
                                                  SYNC_GATEWAY_NAME,
                                                  "paul_id",
                                                  "paul",
                                                  "password1",
                                                  admin_channels=["cars", "paul"])

            couchbase_utils.add_person_to_gateway(admin_url,
                                                  SYNC_GATEWAY_NAME,
                                                  "sol_id",
                                                  "sol",
                                                  "password2",
                                                  admin_channels=["sol"])

            paul_url = "http://*****:*****@%s:%s" % (SYNC_GATEWAY_HOST, SYNC_GATEWAY_PORT)
            self.paul_db = SyncGatewayWrapper(self.mapper, paul_url, SYNC_GATEWAY_NAME)

            sol_url = "http://*****:*****@%s:%s" % (SYNC_GATEWAY_HOST, SYNC_GATEWAY_PORT)
            self.sol_db = SyncGatewayWrapper(self.mapper, sol_url, SYNC_GATEWAY_NAME)
Example #6
0
class testPermissions(unittest.TestCase):

        def setUp(self):

            self.gateway = None
            self.db = None

            self.mapper = ClassMapper([Car, Boat])



        def start_gateway(self, conf_file_path):

            cmd = "{} -log=* {}".format(SYNC_GATEWAY_PATH, conf_file_path)
            print cmd

            time.sleep(0.25)
            self.gateway = subprocess.Popen(cmd, shell=True)
            time.sleep(0.25)

            admin_url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST, SYNC_GATEWAY_ADMIN_PORT)
            self.admin_db = SyncGatewayWrapper(self.mapper, admin_url, SYNC_GATEWAY_NAME)
            self.admin_db.update_designs()
            self.add_users()


        def add_users(self):

            admin_url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST, SYNC_GATEWAY_ADMIN_PORT)

            couchbase_utils.add_person_to_gateway(admin_url,
                                                  SYNC_GATEWAY_NAME,
                                                  "paul_id",
                                                  "paul",
                                                  "password1",
                                                  admin_channels=["cars", "paul"])

            couchbase_utils.add_person_to_gateway(admin_url,
                                                  SYNC_GATEWAY_NAME,
                                                  "sol_id",
                                                  "sol",
                                                  "password2",
                                                  admin_channels=["sol"])

            paul_url = "http://*****:*****@%s:%s" % (SYNC_GATEWAY_HOST, SYNC_GATEWAY_PORT)
            self.paul_db = SyncGatewayWrapper(self.mapper, paul_url, SYNC_GATEWAY_NAME)

            sol_url = "http://*****:*****@%s:%s" % (SYNC_GATEWAY_HOST, SYNC_GATEWAY_PORT)
            self.sol_db = SyncGatewayWrapper(self.mapper, sol_url, SYNC_GATEWAY_NAME)


        def tearDown(self):
            # stop the gateway
            if self.gateway is not None:
                self.gateway.kill()


        def test_index_permissions(self):

            expected = {
                "create": {
                    "car": {
                        "owner": True
                    },
                    "boat": {
                        "owner": True,
                        "withoutAccess": True
                    }
                },
                "update": {
                    "car": [
                        {
                            "fields": [
                                "access"
                            ],
                            "role": []
                        },
                        {
                            "owner": True,
                            "fields": [
                                "colour"
                            ]
                        }
                    ],
                    "boat": [
                        {
                            "role": []
                        }
                    ]
                },
                "delete": {
                    "car": {
                        "owner": True
                    },
                    "boat": {
                        "owner": True
                    }
                }
            }



            requirements = _requirements_from_mapper(self.mapper)

            print json.dumps(requirements, indent=4)

            self.assertEqual(expected, requirements)


        def test_write_permissions(self):

            src_path = os.path.join(DATA_PATH, "sync_conf_template")
            dst_path = os.path.join(DATA_PATH, "sync_conf")

            if os.path.exists(dst_path):
                os.remove(dst_path)

            write_sync_function(src_path, dst_path, self.mapper)

            self.start_gateway(dst_path)


        def test_owner_create(self):

            self.test_write_permissions()

            car = Car(colour="red", stars=3, owner_name="paul", channels=["cars", "sol"])
            self.paul_db.put(car)

            car.colour = "green"
            self.paul_db.put(car)
            car.stars = 2
            self.paul_db.put(car)
            self.paul_db.delete(car)

            car2 = Car(colour="green", owner_name="sol", channels=["cars", "sol"])
            self.assertRaises(FamResourceConflict, self.paul_db.put, car2)


        def test_non_owner_permissions(self):

            self.test_write_permissions()

            car = Car(colour="red", stars=3, owner_name="paul", channels=["cars", "sol"])
            self.paul_db.put(car)
            sols_car = self.sol_db.get(car.key)

            ## changing green fails
            sols_car.colour = "green"
            self.assertRaises(FamResourceConflict, self.sol_db.put, sols_car)

            ## changing stars works
            sols_car.colour = "red"
            sols_car.stars = 2
            self.sol_db.put(sols_car)

            # check nn owner cant delete
            self.assertRaises(FamResourceConflict, self.sol_db.delete, sols_car)


        def test_wrong_owner_create_fails(self):

            self.test_write_permissions()
            car = Car(colour="red", stars=3, owner_name="sol", channels=["cars", "sol"])
            self.assertRaises(FamResourceConflict, self.paul_db.put, car)


        def test_create_access(self):
            self.test_write_permissions()
            car1 = Car(colour="red", stars=3, owner_name="sol", channels=["sol"])
            self.sol_db.put(car1)
            self.sol_db.get(car1.key)

            car2 = Car(colour="green", stars=2, owner_name="sol", channels=["paul"])
            self.assertRaises(Exception, self.sol_db.put, car2)
            self.sol_db.get(car2.key)


        def test_change_access_without_permission_fails(self):
            self.test_write_permissions()
            car = Car(colour="red", stars=3, owner_name="paul", channels=["cars"])
            self.paul_db.put(car)
            car.access = ["sol"]
            self.assertRaises(FamResourceConflict, self.paul_db.put, car)


        def test_change_access(self):
            self.test_write_permissions()
            car = Car(key="cars", colour="red", stars=3, owner_name="paul", channels=["cars"])
            self.paul_db.put(car)

            ## sol cant get car
            self.assertRaises(Exception, self.sol_db.get, "cars")

            car.access = ["sol"]
            self.admin_db.put(car)
            self.sol_db.get(car.key)


        def test_no_access(self):
            self.test_write_permissions()
            bike = Bike(wheels = 2)
            self.assertRaises(Exception, self.paul_db.put, bike)


        def test_no_access_admin(self):
            self.test_write_permissions()
            bike = Bike(wheels=2)
            self.admin_db.put(bike)


        def test_own_access_create(self):

            self.test_write_permissions()
            boat_id = "boaty"
            boat = Boat(key=boat_id, name="steve", is_sail=True, owner_name="paul", access=["paul"], channels=[boat_id])
            self.paul_db.put(boat)
Example #7
0
class BasicTests(unittest.TestCase):
    def setUp(self):
        cmd = "{} -log=* -url walrus: ".format(SYNC_GATEWAY_PATH)
        # print cmd

        time.sleep(0.25)
        self.gateway = subprocess.Popen(cmd, shell=True)
        time.sleep(0.25)
        filepath = os.path.join(DATA_PATH, "animal_views.js")
        mapper = ClassMapper(
            [Dog, Cat, Person, JackRussell, Monkey, Monarch, Monster],
            designs=[filepath])

        url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST,
                                SYNC_GATEWAY_ADMIN_PORT)
        self.db = SyncGatewayWrapper(mapper, url, SYNC_GATEWAY_NAME)
        self.db.update_designs()
        super(self.__class__, self).setUp()

    def tearDown(self):
        # stop the gateway
        self.gateway.kill()

    # Test the app
    def test_app(self):
        self.assertNotEqual(self.db, None)

    def test_make_an_object(self):
        dog = Dog(name="woofer")
        self.assertEqual(dog.namespace, NAMESPACE)
        self.assertEqual(dog.type, "dog")
        self.assertEqual(dog.name, "woofer")
        self.assertEqual(dog.__class__, Dog)

    def test_make_an_object2(self):
        dog = Dog(name="woofer")
        self.assertEqual(dog.talk(), "woof")

    def test_make_a_sub_object(self):
        jack = JackRussell()
        self.assertEqual(jack.talk(), "Yap")
        jack.name = "jack"
        jack.age = 12
        self.db.put(jack)

    def test_make_an_object_saved(self):
        dog = Dog(name="woofer")
        dog.save(self.db)
        self.assertEqual(dog.namespace, NAMESPACE)
        self.assertEqual(dog.type, "dog")
        self.assertEqual(dog.name, "woofer")
        self.assertEqual(dog.__class__, Dog)
        self.assertNotEqual(dog.key, None)
        # self.assertTrue(dog.schema.startswith("http://glowinthedark.co.uk/test/dog"))

    def test_make_an_object_double_dispatch_saved(self):
        dog = Dog(name="woofer")
        self.db.put(dog)
        self.assertEqual(dog.namespace, NAMESPACE)
        self.assertEqual(dog.type, "dog")
        self.assertEqual(dog.name, "woofer")
        self.assertEqual(dog.__class__, Dog)
        self.assertNotEqual(dog.key, None)

    def test_make_an_object_with_additional_properties(self):
        dog = Dog(name="woofer", collar="leather")
        dog.home = "hackney"
        self.db.put(dog)
        self.assertEqual(dog.home, "hackney")

    def test_fail_with_additional_properties(self):
        def wrong_monkey():
            return Monkey(name="bonzo", collar="leather")

        def collar_monkey(monkey):
            monkey.collar = "leather"

        self.assertRaises(FamValidationError, wrong_monkey)
        monkey = Monkey(name="bonzo")
        self.assertRaises(FamValidationError, collar_monkey, monkey)

    def test_make_an_object_saved_cas(self):
        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=2)
        cat.save(self.db)
        self.assertEqual(cat.namespace, NAMESPACE)
        self.assertEqual(cat.type, "cat")
        self.assertNotEqual(cat.rev, None)

    def test_get_cat(self):
        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=2)
        cat.save(self.db)
        another = Cat.get(self.db, cat.key)
        self.assertEqual(cat, another)

    def test_ref_to(self):
        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=2)
        cat.save(self.db)
        self.assertEqual(cat.owner, paul)
        self.assertEqual(cat.owner.name, "paul")

    def test_ref_to_with_object(self):
        paul = Person(name="paul")
        self.db.put(paul)
        cat = Cat(name="whiskers", owner=paul, legs=2)
        cat.save(self.db)
        self.assertEqual(cat.owner, paul)
        self.assertEqual(cat.owner.name, "paul")
        catback = self.db.get(cat.key)
        self.assertEqual(cat, catback)

    def test_ref_from(self):
        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=2)
        cat.save(self.db)
        cat2 = Cat(name="puss", owner_id=paul.key, legs=2)
        cat2.save(self.db)
        cats = list(paul.cats)
        self.assertEqual(len(cats), 2)
        cats = list(paul.cats)
        self.assertTrue(cats[0] == cat or cats[1] == cat)

    def test_ref_from_multiple_index(self):
        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=2)
        cat.save(self.db)
        dog = Dog(name="fly", owner=paul)
        self.db.put(dog)
        cats = list(paul.cats)
        self.assertEqual(len(cats), 1)
        dogs = list(paul.dogs)
        self.assertEqual(len(dogs), 1)
        animals = list(paul.animals)
        self.assertEqual(len(animals), 2)

    def test_refs_with_inheritance(self):
        paul = Person(name="paul")
        paul.save(self.db)
        jack = JackRussell()
        jack.owner_id = paul.key
        jack.name = "jack"
        jack.save(self.db)

        self.assertEqual(list(paul.dogs)[0], jack)

    def test_refs_with_other_inheritance(self):
        paul = Monarch(name="paul")
        self.db.put(paul)
        jack = Dog()
        jack.owner = paul
        jack.name = "jack"
        self.db.put(jack)
        dogs = list(paul.dogs)
        self.assertEqual(dogs[0], jack)

    def test_delete_cat_dd(self):
        paul = Person(name="paul")
        self.db.put(paul)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=2)
        self.db.put(cat)
        key = cat.key
        another = self.db.get(cat.key)
        self.assertEqual(cat, another)
        self.db.delete(cat)
        revivedcat = self.db.get(key)
        self.assertEqual(revivedcat, None)

    def test_delete_dog_to_refs(self):
        paul = Person(name="paul")
        paul.save(self.db)
        dog = Dog(name="rufus", owner_id=paul.key)
        dog.save(self.db)
        key = dog.key
        dog2 = Dog(name="fly", owner_id=paul.key)
        dog2.save(self.db)
        self.assertTrue(dog2.owner is not None)
        key = paul.key
        dog.delete(self.db)
        revivedpaul = self.db.get(key)
        self.assertTrue(revivedpaul is None)
        refresheddog2 = Dog.get(self.db, dog2.key)
        self.assertTrue(refresheddog2.owner is None)

    def test_delete_cat_refs(self):
        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=2)
        cat.save(self.db)
        key = cat.key
        cat2 = Cat(name="puss", owner_id=paul.key, legs=2)
        cat2.save(self.db)

        cats = list(paul.cats)

        # revivedcat1 = self.db.get(key)
        #
        # self.assertTrue(revivedcat1 is not None)
        #
        # paul.delete(self.db)
        # revivedcat2 = self.db.get(key)
        # self.assertTrue(revivedcat2 is None)

    def test_delete_dog_refs(self):
        paul = Person(name="paul")
        paul.save(self.db)
        dog = Dog(name="rufus", owner_id=paul.key)
        dog.save(self.db)
        key = dog.key
        dog2 = Dog(name="fly", owner_id=paul.key)
        dog2.save(self.db)
        reviveddog1 = self.db.get(key)
        self.assertTrue(reviveddog1 is not None)
        paul.delete(self.db)
        reviveddog2 = self.db.get(key)
        self.assertTrue(reviveddog2 is not None)

    def test_update_cat(self):
        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=2)
        cat.save(self.db)
        cat.name = "blackie"
        cat.save(self.db)
        self.assertEqual(cat.name, "blackie")
        self.assertEqual(cat._properties["name"], "blackie")
        self.assertFalse("name" in cat.__dict__.keys())

    def test_update_cat_fails(self):
        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=2)
        cat.save(self.db)
        cat.colour = "black"
        cat.save(self.db)

        def change_colour():
            cat.colour = "white"

        self.assertRaises(FamImmutableError, change_colour)

    def setcatfood(self):
        self.cat.food = "biscuits"

    def test_update_catfood(self):
        paul = Person(name="paul")
        paul.save(self.db)
        cat = Cat(name="whiskers", owner_id=paul.key, legs=2)
        cat.save(self.db)
        self.assertRaises(Exception, self.setcatfood)

    def test_update_dogfood(self):
        dog = Dog(name="fly")
        dog.food = "biscuits"
        dog.save(self.db)

    def test_update_fails_without_rev(self):
        dog = Dog(name="fly")
        dog.save(self.db)
        dog.rev = None
        dog.name = "jess"
        self.assertRaises(FamResourceConflict, self.db.put, dog)

    def test_update_works_without_rev(self):
        monkey = Monkey(name="fly")
        monkey.save(self.db)
        monkey.rev = None
        monkey.name = "jess"
        self.db.put(monkey)

    def test_uniqueness(self):

        paul = Person(name="paul")
        self.db.put(paul)
        dog1 = Dog(name="rufus",
                   owner_id=paul.key,
                   kennel_club_membership="123456")

        dog1.save(self.db)
        # time.sleep(1)

        # print dog1.as_json()
        # dog2 = Dog(name="fly", owner_id=paul.key, kennel_club_membership="123456")
        # print dog2.as_json()
        # # self.db.put(dog2)
        # self.assertRaises(FamUniqueError, self.db.put, dog2)
        # # print "*********** end ***************"


# class RefNameTests(unittest.TestCase):
#
#     # this test that refs whos name doesnt end with _id fail
#     def test_misnamed_ref_to_fails(self):
#
#         def duff_import():
#             from fam.tests.models import test02
#             print(test02)
#
#         self.assertRaises(FamError, duff_import)
Example #8
0
class testPermissions(unittest.TestCase):
    def setUp(self):

        self.gateway = None
        self.db = None

        self.mapper = ClassMapper([Car, Boat])

        self.start_gateway()

    def start_gateway(self):

        cmd = "{} -log=* -url walrus: ".format(SYNC_GATEWAY_PATH)
        # print cmd

        time.sleep(0.25)
        self.gateway = subprocess.Popen(cmd, shell=True)
        time.sleep(0.25)

        admin_url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST,
                                      SYNC_GATEWAY_ADMIN_PORT)
        self.admin_db = SyncGatewayWrapper(self.mapper, admin_url,
                                           SYNC_GATEWAY_NAME)
        self.admin_db.update_designs()
        self.add_users()

    def add_users(self):

        admin_url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST,
                                      SYNC_GATEWAY_ADMIN_PORT)

        couchbase_utils.add_person_to_gateway(admin_url,
                                              SYNC_GATEWAY_NAME,
                                              "paul_id",
                                              "paul",
                                              "password1",
                                              admin_channels=["cars", "paul"])

        couchbase_utils.add_person_to_gateway(admin_url,
                                              SYNC_GATEWAY_NAME,
                                              "sol_id",
                                              "sol",
                                              "password2",
                                              admin_channels=["sol"])

        paul_url = "http://*****:*****@%s:%s" % (SYNC_GATEWAY_HOST,
                                                    SYNC_GATEWAY_PORT)
        self.paul_db = SyncGatewayWrapper(self.mapper, paul_url,
                                          SYNC_GATEWAY_NAME)

        sol_url = "http://*****:*****@%s:%s" % (SYNC_GATEWAY_HOST,
                                                  SYNC_GATEWAY_PORT)
        self.sol_db = SyncGatewayWrapper(self.mapper, sol_url,
                                         SYNC_GATEWAY_NAME)

    def tearDown(self):
        # stop the gateway
        if self.gateway is not None:
            self.gateway.kill()

    def test_get_user(self):

        user_info = self.admin_db.user("paul")
        # print user_info
        self.assertTrue(user_info != None)
        roles = user_info["admin_roles"]

        self.assertEqual(roles, ["paul_id"])

    def test_create_role(self):

        role_info = self.admin_db.role("new_role")
        # print "role: ",  role_info
        self.assertTrue(role_info == None)

        self.admin_db.ensure_role("new_role")

        role_info = self.admin_db.role("new_role")
        # print "role: ",  role_info
        self.assertTrue(role_info != None)

    def test_add_role(self):

        user_info = self.admin_db.user("paul")
        self.assertTrue(user_info != None)
        roles = user_info["admin_roles"]
        self.assertEqual(roles, ["paul_id"])
        channels = user_info["admin_channels"]
        self.assertEqual(set(channels), set(["cars", "paul"]))

        success = self.admin_db.ensure_user_role("paul", "new_role")

        self.assertTrue(success)

        user_info = self.admin_db.user("paul")
        self.assertTrue(user_info != None)
        roles = user_info["admin_roles"]
        channels = user_info["admin_channels"]
        self.assertEqual(set(roles), set(["paul_id", "new_role"]))
        self.assertEqual(set(channels), set(["cars", "paul"]))
Example #9
0
class testPermissions(unittest.TestCase):

        def setUp(self):

            self.gateway = None
            self.db = None

            self.mapper = ClassMapper([Cat, Dog])

            self.start_gateway()



        def start_gateway(self):

            cmd = "{} -log=* -url walrus: ".format(SYNC_GATEWAY_PATH)
            # print cmd

            time.sleep(0.25)
            self.gateway = subprocess.Popen(cmd, shell=True)
            time.sleep(0.25)

            admin_url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST, SYNC_GATEWAY_ADMIN_PORT)
            self.admin_db = SyncGatewayWrapper(self.mapper, admin_url, SYNC_GATEWAY_NAME)
            self.admin_db.update_designs()
            self.add_users()


        def add_users(self):

            admin_url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST, SYNC_GATEWAY_ADMIN_PORT)

            couchbase_utils.add_person_to_gateway(admin_url,
                                                  SYNC_GATEWAY_NAME,
                                                  "paul_id",
                                                  "paul",
                                                  "password1",
                                                  admin_channels=["cars", "paul"])

            couchbase_utils.add_person_to_gateway(admin_url,
                                                  SYNC_GATEWAY_NAME,
                                                  "sol_id",
                                                  "sol",
                                                  "password2",
                                                  admin_channels=["sol"])

            paul_url = "http://*****:*****@%s:%s" % (SYNC_GATEWAY_HOST, SYNC_GATEWAY_PORT)
            self.paul_db = SyncGatewayWrapper(self.mapper, paul_url, SYNC_GATEWAY_NAME)

            sol_url = "http://*****:*****@%s:%s" % (SYNC_GATEWAY_HOST, SYNC_GATEWAY_PORT)
            self.sol_db = SyncGatewayWrapper(self.mapper, sol_url, SYNC_GATEWAY_NAME)


        def tearDown(self):
            # stop the gateway
            if self.gateway is not None:
                self.gateway.kill()


        def test_ensure_designs(self):

            for namespace_name, namespace in self.admin_db.mapper.namespaces.items():
                view_namespace = namespace_name.replace("/", "_")
                key = "_design/%s" % view_namespace
                doc = self.mapper.get_design(namespace, namespace_name, self.admin_db.FOREIGN_KEY_MAP_STRING)
                doc["_id"] = key
                existing = self.admin_db.get_design(key)
                matches = self.admin_db._new_matches_existing(doc, existing)
                self.assertTrue(matches)

            # Add another class
            self.admin_db.mapper._add_classes([Person])

            for namespace_name, namespace in self.admin_db.mapper.namespaces.items():
                view_namespace = namespace_name.replace("/", "_")
                key = "_design/%s" % view_namespace
                doc = self.mapper.get_design(namespace, namespace_name, self.admin_db.FOREIGN_KEY_MAP_STRING)
                doc["_id"] = key
                existing = self.admin_db.get_design(key)
                matches = self.admin_db._new_matches_existing(doc, existing)

                self.assertFalse(matches)
Example #10
0
class testPermissions(unittest.TestCase):

        def setUp(self):

            self.gateway = None
            self.db = None

            self.mapper = ClassMapper([Car, Boat])

            self.start_gateway()



        def start_gateway(self):

            cmd = "{} -log=* -url walrus: ".format(SYNC_GATEWAY_PATH)
            # print cmd

            time.sleep(0.25)
            self.gateway = subprocess.Popen(cmd, shell=True)
            time.sleep(0.25)

            admin_url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST, SYNC_GATEWAY_ADMIN_PORT)
            self.admin_db = SyncGatewayWrapper(self.mapper, admin_url, SYNC_GATEWAY_NAME)
            self.admin_db.update_designs()
            self.add_users()


        def add_users(self):

            admin_url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST, SYNC_GATEWAY_ADMIN_PORT)

            couchbase_utils.add_person_to_gateway(admin_url,
                                                  SYNC_GATEWAY_NAME,
                                                  "paul_id",
                                                  "paul",
                                                  "password1",
                                                  admin_channels=["cars", "paul"])

            couchbase_utils.add_person_to_gateway(admin_url,
                                                  SYNC_GATEWAY_NAME,
                                                  "sol_id",
                                                  "sol",
                                                  "password2",
                                                  admin_channels=["sol"])

            paul_url = "http://*****:*****@%s:%s" % (SYNC_GATEWAY_HOST, SYNC_GATEWAY_PORT)
            self.paul_db = SyncGatewayWrapper(self.mapper, paul_url, SYNC_GATEWAY_NAME)

            sol_url = "http://*****:*****@%s:%s" % (SYNC_GATEWAY_HOST, SYNC_GATEWAY_PORT)
            self.sol_db = SyncGatewayWrapper(self.mapper, sol_url, SYNC_GATEWAY_NAME)


        def tearDown(self):
            # stop the gateway
            if self.gateway is not None:
                self.gateway.kill()


        def test_get_user(self):

            user_info = self.admin_db.user("paul")
            # print user_info
            self.assertTrue(user_info != None)
            roles = user_info["admin_roles"]

            self.assertEqual(roles, ["paul_id"])


        def test_create_role(self):

            role_info = self.admin_db.role("new_role")
            # print "role: ",  role_info
            self.assertTrue(role_info == None)

            self.admin_db.ensure_role("new_role")

            role_info = self.admin_db.role("new_role")
            # print "role: ",  role_info
            self.assertTrue(role_info != None)


        def test_add_role(self):

            user_info = self.admin_db.user("paul")
            self.assertTrue(user_info != None)
            roles = user_info["admin_roles"]
            self.assertEqual(roles, ["paul_id"])
            channels = user_info["admin_channels"]
            self.assertEqual(set(channels), set(["cars", "paul"]))

            success = self.admin_db.ensure_user_role("paul", "new_role")

            self.assertTrue(success)

            user_info = self.admin_db.user("paul")
            self.assertTrue(user_info != None)
            roles = user_info["admin_roles"]
            channels = user_info["admin_channels"]
            self.assertEqual(set(roles), set(["paul_id", "new_role"]))
            self.assertEqual(set(channels), set(["cars", "paul"]))
Example #11
0
class testPermissions(unittest.TestCase):
    def setUp(self):

        self.gateway = None
        self.db = None

        self.mapper = ClassMapper([Car, Boat])

    def start_gateway(self, conf_file_path):

        cmd = "{} -log=* {}".format(SYNC_GATEWAY_PATH, conf_file_path)
        print cmd

        time.sleep(0.25)
        self.gateway = subprocess.Popen(cmd, shell=True)
        time.sleep(0.25)

        admin_url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST,
                                      SYNC_GATEWAY_ADMIN_PORT)
        self.admin_db = SyncGatewayWrapper(self.mapper, admin_url,
                                           SYNC_GATEWAY_NAME)
        self.admin_db.update_designs()
        self.add_users()

    def add_users(self):

        admin_url = "http://%s:%s" % (SYNC_GATEWAY_ADMIN_HOST,
                                      SYNC_GATEWAY_ADMIN_PORT)

        couchbase_utils.add_person_to_gateway(admin_url,
                                              SYNC_GATEWAY_NAME,
                                              "paul_id",
                                              "paul",
                                              "password1",
                                              admin_channels=["cars", "paul"])

        couchbase_utils.add_person_to_gateway(admin_url,
                                              SYNC_GATEWAY_NAME,
                                              "sol_id",
                                              "sol",
                                              "password2",
                                              admin_channels=["sol"])

        paul_url = "http://*****:*****@%s:%s" % (SYNC_GATEWAY_HOST,
                                                    SYNC_GATEWAY_PORT)
        self.paul_db = SyncGatewayWrapper(self.mapper, paul_url,
                                          SYNC_GATEWAY_NAME)

        sol_url = "http://*****:*****@%s:%s" % (SYNC_GATEWAY_HOST,
                                                  SYNC_GATEWAY_PORT)
        self.sol_db = SyncGatewayWrapper(self.mapper, sol_url,
                                         SYNC_GATEWAY_NAME)

    def tearDown(self):
        # stop the gateway
        if self.gateway is not None:
            self.gateway.kill()

    def test_index_permissions(self):

        expected = {
            "create": {
                "car": {
                    "owner": True
                },
                "boat": {
                    "owner": True,
                    "withoutAccess": True
                }
            },
            "update": {
                "car": [{
                    "fields": ["access"],
                    "role": []
                }, {
                    "owner": True,
                    "fields": ["colour"]
                }],
                "boat": [{
                    "role": []
                }]
            },
            "delete": {
                "car": {
                    "owner": True
                },
                "boat": {
                    "owner": True
                }
            }
        }

        requirements = _requirements_from_mapper(self.mapper)

        print json.dumps(requirements, indent=4)

        self.assertEqual(expected, requirements)

    def test_write_permissions(self):

        src_path = os.path.join(DATA_PATH, "sync_conf_template")
        dst_path = os.path.join(DATA_PATH, "sync_conf")

        if os.path.exists(dst_path):
            os.remove(dst_path)

        write_sync_function(src_path, dst_path, self.mapper)

        self.start_gateway(dst_path)

    def test_owner_create(self):

        self.test_write_permissions()

        car = Car(colour="red",
                  stars=3,
                  owner_name="paul",
                  channels=["cars", "sol"])
        self.paul_db.put(car)

        car.colour = "green"
        self.paul_db.put(car)
        car.stars = 2
        self.paul_db.put(car)
        self.paul_db.delete(car)

        car2 = Car(colour="green", owner_name="sol", channels=["cars", "sol"])
        self.assertRaises(FamResourceConflict, self.paul_db.put, car2)

    def test_non_owner_permissions(self):

        self.test_write_permissions()

        car = Car(colour="red",
                  stars=3,
                  owner_name="paul",
                  channels=["cars", "sol"])
        self.paul_db.put(car)
        sols_car = self.sol_db.get(car.key)

        ## changing green fails
        sols_car.colour = "green"
        self.assertRaises(FamResourceConflict, self.sol_db.put, sols_car)

        ## changing stars works
        sols_car.colour = "red"
        sols_car.stars = 2
        self.sol_db.put(sols_car)

        # check nn owner cant delete
        self.assertRaises(FamResourceConflict, self.sol_db.delete, sols_car)

    def test_wrong_owner_create_fails(self):

        self.test_write_permissions()
        car = Car(colour="red",
                  stars=3,
                  owner_name="sol",
                  channels=["cars", "sol"])
        self.assertRaises(FamResourceConflict, self.paul_db.put, car)

    def test_create_access(self):
        self.test_write_permissions()
        car1 = Car(colour="red", stars=3, owner_name="sol", channels=["sol"])
        self.sol_db.put(car1)
        self.sol_db.get(car1.key)

        car2 = Car(colour="green",
                   stars=2,
                   owner_name="sol",
                   channels=["paul"])
        self.assertRaises(Exception, self.sol_db.put, car2)
        self.sol_db.get(car2.key)

    def test_change_access_without_permission_fails(self):
        self.test_write_permissions()
        car = Car(colour="red", stars=3, owner_name="paul", channels=["cars"])
        self.paul_db.put(car)
        car.access = ["sol"]
        self.assertRaises(FamResourceConflict, self.paul_db.put, car)

    def test_change_access(self):
        self.test_write_permissions()
        car = Car(key="cars",
                  colour="red",
                  stars=3,
                  owner_name="paul",
                  channels=["cars"])
        self.paul_db.put(car)

        ## sol cant get car
        self.assertRaises(Exception, self.sol_db.get, "cars")

        car.access = ["sol"]
        self.admin_db.put(car)
        self.sol_db.get(car.key)

    def test_no_access(self):
        self.test_write_permissions()
        bike = Bike(wheels=2)
        self.assertRaises(Exception, self.paul_db.put, bike)

    def test_no_access_admin(self):
        self.test_write_permissions()
        bike = Bike(wheels=2)
        self.admin_db.put(bike)

    def test_own_access_create(self):

        self.test_write_permissions()
        boat_id = "boaty"
        boat = Boat(key=boat_id,
                    name="steve",
                    is_sail=True,
                    owner_name="paul",
                    access=["paul"],
                    channels=[boat_id])
        self.paul_db.put(boat)