예제 #1
0
    def setUp(self):
        self.data_store = DataStore()
        self.order = Order(self.data_store)
        self.app = Flask(__name__)
        self.base_uber_api = UbersmithBase(self.data_store)

        self.order.hook_to(self.base_uber_api)
        self.base_uber_api.hook_to(self.app)
예제 #2
0
    def setUp(self):
        self.data_store = DataStore()
        self.iweb = IWeb(self.data_store)

        self.app = Flask(__name__)
        self.base_iweb_api = UbersmithBase(self.data_store)

        self.iweb.hook_to(self.base_iweb_api)
        self.base_iweb_api.hook_to(self.app)
예제 #3
0
    def setUp(self):
        self.data_store = DataStore()
        self.client = Client(self.data_store)

        self.app = Flask(__name__)
        self.base_uber_api = UbersmithBase(self.data_store)

        self.client.hook_to(self.base_uber_api)
        self.base_uber_api.hook_to(self.app)
예제 #4
0
class TestAdministrativeLocal(unittest.TestCase):
    def setUp(self):
        self.app = Flask(__name__)
        data_store = DataStore()

        self.ubersmith_base = UbersmithBase(data_store)
        self.ubersmith_base.hook_to(self.app)

    def test_enable_crash_mode(self):
        with self.app.test_client() as c:
            resp = c.post(
                'api/2.0/',
                data={
                    "method": "hidden.enable_crash_mode",
                }
            )

        self.assertEqual(resp.status_code, 200)
        self.assertTrue(self.ubersmith_base.crash_mode)

    def test_disable_crash_mode(self):
        self.ubersmith_base.crash_mode = True

        with self.app.test_client() as c:
            resp = c.post(
                'api/2.0/',
                data={
                    "method": "hidden.disable_crash_mode",
                }
            )

        self.assertEqual(resp.status_code, 200)
        self.assertFalse(self.ubersmith_base.crash_mode)

    def test_any_api_call_will_return_500_when_crash_mode_enabled(self):
        self.ubersmith_base.crash_mode = True

        with self.app.test_client() as c:
            resp = c.post(
                'api/2.0/',
                data={
                    "method": "any.api_call",
                }
            )

        self.assertEqual(resp.status_code, 500)
예제 #5
0
def run():
    # TODO (wajdi) Make configurable passed parameter
    port = 9131

    app = Flask('fake_ubersmith')

    data_store = DataStore()
    base_uber_api = UbersmithBase(data_store)

    AdministrativeLocal().hook_to(app)

    Uber(data_store).hook_to(base_uber_api)
    Order(data_store).hook_to(base_uber_api)
    Client(data_store).hook_to(base_uber_api)

    base_uber_api.hook_to(app)

    _get_logger(app)

    app.run(host="0.0.0.0", port=port)
예제 #6
0
class TestIwebModule(unittest.TestCase):
    def setUp(self):
        self.data_store = DataStore()
        self.iweb = IWeb(self.data_store)

        self.app = Flask(__name__)
        self.base_iweb_api = UbersmithBase(self.data_store)

        self.iweb.hook_to(self.base_iweb_api)
        self.base_iweb_api.hook_to(self.app)

    def test_log_event_successfully(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "iweb.log_event",
                              "event_type": "Some event type",
                              "reference_type": "client",
                              "action":
                              "Client Id 12345 performed a 'method'.",
                              "clientid": "clientid",
                              "user": "******",
                              "reference_id": "clientid"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.data.decode('utf-8')), {
            "data": "1",
            "error_code": None,
            "error_message": "",
            "status": True
        })
        self.assertEqual(
            self.data_store.event_log[0], {
                "event_type": "Some event type",
                "reference_type": "client",
                "action": "Client Id 12345 performed a 'method'.",
                "clientid": "clientid",
                "user": "******",
                "reference_id": "clientid"
            })
예제 #7
0
class TestIwebModule(unittest.TestCase):
    def setUp(self):
        self.data_store = DataStore()
        self.iweb = IWeb(self.data_store)

        self.app = Flask(__name__)
        self.base_iweb_api = UbersmithBase(self.data_store)

        self.iweb.hook_to(self.base_iweb_api)
        self.base_iweb_api.hook_to(self.app)

    def test_log_event_successfully(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "iweb.log_event",
                              "event_type": "Some event type",
                              "reference_type": "client",
                              "action":
                              "Client Id 12345 performed a 'method'.",
                              "clientid": "clientid",
                              "user": "******",
                              "reference_id": "clientid"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.data.decode('utf-8')), {
            "data": "1",
            "error_code": None,
            "error_message": "",
            "status": True
        })
        self.assertEqual(
            self.data_store.event_log[0], {
                "event_type": "Some event type",
                "reference_type": "client",
                "action": "Client Id 12345 performed a 'method'.",
                "clientid": "clientid",
                "user": "******",
                "reference_id": "clientid"
            })

    def test_add_role_successfully(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "iweb.acl_admin_role_add",
                              'name': 'A Admin Role',
                              'descr': 'A Admin Role',
                              'acls[admin.portal][read]': 1,
                          })

        role_id = next(iter(self.data_store.roles))

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": role_id,
                "error_code": None,
                "error_message": "",
                "status": True
            })
        self.assertEqual(
            self.data_store.roles, {
                role_id: {
                    'role_id': role_id,
                    'name': 'A Admin Role',
                    'descr': 'A Admin Role',
                    'acls': {
                        'admin.portal': {
                            'read': '1'
                        }
                    }
                }
            })

    def test_add_role_that_already_exists_fails(self):
        self.data_store.roles = {
            'some_role_id': {
                'role_id': 'some_role_id',
                'name': 'A Admin Role'
            }
        }

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "iweb.acl_admin_role_add",
                              'name': 'A Admin Role',
                              'descr': 'A Admin Role',
                              'acls[admin.portal][read]': 1,
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 1,
                "error_message": "The specified Role Name is already in use",
                "status": False
            })

    def test_add_user_role_successfully(self):
        user_id = 'some_user_id'
        self.data_store.roles = {'a_role_id': {}, 'another_role_id': {}}
        with self.app.test_client() as c:
            c.post('api/2.0/',
                   data={
                       "method": "iweb.user_role_assign",
                       "user_id": user_id,
                       "role_id": "a_role_id"
                   })
            resp = c.post('api/2.0/',
                          data={
                              "method": "iweb.user_role_assign",
                              "user_id": user_id,
                              "role_id": "another_role_id"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.data.decode('utf-8')), {
            "status": True,
            "error_code": None,
            "error_message": "",
            "data": 1
        })
        self.assertEqual(self.data_store.user_mapping[user_id],
                         {'roles': {'a_role_id', 'another_role_id'}})

    def test_add_same_role_to_user_not_allowed(self):
        role_id = 'some_role_id'
        user_id = 'some_user_id'
        self.data_store.roles = {'1': {}}
        self.data_store.user_mapping[user_id] = {'roles': {role_id}}
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "iweb.user_role_assign",
                              "user_id": user_id,
                              "role_id": role_id
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "error_code":
                1,
                "error_message":
                "Can't assign role with id '{}' to user "
                "with id '{}'".format(role_id, user_id),
                "status":
                False,
                "data":
                ""
            })
예제 #8
0
class TestOrderModule(unittest.TestCase):
    def setUp(self):
        self.data_store = DataStore()
        self.order = Order(self.data_store)
        self.app = Flask(__name__)
        self.base_uber_api = UbersmithBase(self.data_store)

        self.order.hook_to(self.base_uber_api)
        self.base_uber_api.hook_to(self.app)

    def test_coupon_get_returns_successfully(self):
        self.data_store.coupons = [{
            "coupon": {
                "coupon_code": "1"
            }
        }, {
            "coupon": {
                "coupon_code": "2"
            }
        }]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "order.coupon_get",
                              "coupon_code": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertDictEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "coupon": {
                        "coupon_code": "1"
                    }
                },
                "error_message": "",
                "status": True,
                "error_code": None,
            })

    def test_coupon_get_returns_error(self):
        self.data_store.coupons = [{
            "coupon": {
                "coupon_code": "100"
            }
        }, {
            "coupon": {
                "coupon_code": "200"
            }
        }]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "order.coupon_get",
                              "coupon_code": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "status": False,
                "error_message": "could not get coupon info",
                "error_code": 1
            })

    def test_create_order_succeeds(self):
        self.data_store.order = {
            "1": {
                "order_id": "100",
                "order_queue_id": "1"
            }
        }

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "order.create",
                              "order_queue_id": "1"
                          })
        self.assertEqual(resp.status_code, 200)

        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "order_id": "100",
                    "order_queue_id": "1"
                },
                "status": True,
                "error_message": "",
                "error_code": None
            })

    def test_create_order_errs(self):
        self.data_store.order = {
            "1": FakeUbersmithError(code=999, message='epic fail')
        }

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "order.create",
                              "order_queue_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 999,
                "error_message": "epic fail",
                "status": False
            })

    def test_order_respond_responds(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/', data={"method": "order.respond"})
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.data.decode('utf-8')), {
            "data": 8,
            "error_code": None,
            "error_message": "",
            "status": True
        })

    def test_submit_order_is_successful(self):
        self.data_store.order_submit = {"100": {"order_id": "100"}}

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "order.submit",
                              "order_id": "100"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "order_id": "100"
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_submit_order_errs(self):
        self.data_store.order_submit = {
            "100": FakeUbersmithError(code=999, message='epic fail')
        }

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "order.submit",
                              "order_id": "100"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 999,
                "error_message": "epic fail",
                "status": False
            })

    def test_cancel_order_is_successful(self):
        self.data_store.order_cancel = {"100": {"order_id": "100"}}

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "order.cancel",
                              "order_id": "100"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "order_id": "100"
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_cancel_order_errs(self):
        self.data_store.order_cancel = {
            "100": FakeUbersmithError(code=999, message='epic fail')
        }

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "order.cancel",
                              "order_id": "100"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 999,
                "error_message": "epic fail",
                "status": False
            })
예제 #9
0
class TestClientModule(ApiTestBase):
    def setUp(self):
        self.maxDiff = 9001
        self.data_store = DataStore()
        self.client = Client(self.data_store)

        self.app = Flask(__name__)
        self.base_uber_api = UbersmithBase(self.data_store)

        self.client.hook_to(self.base_uber_api)
        self.base_uber_api.hook_to(self.app)

    def test_client_add_creates_a_client(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.add",
                              "first": "John",
                              "last": "Smith",
                              "email": "*****@*****.**",
                              "uber_login": "******",
                              "uber_pass": "******"
                          })

        self.assertEqual(resp.status_code, 200)
        body = json.loads(resp.data.decode('utf-8'))
        self.assertEqual(resp.status_code, 200)
        self.assertIsNone(body.get("error_code"))
        self.assertTrue(body.get("status"))
        self.assertEqual(body.get("error_message"), "")
        self.assertIsInstance(body.get("data"), str)
        self.assertEqual(self.data_store.clients[0]["login"], "john")
        self.assertIsInstance(self.data_store.contacts[0]["contact_id"], str)
        self.assertEqual(self.data_store.contacts[0]["client_id"],
                         body.get("data"))
        self.assertEqual(self.data_store.contacts[0]["description"],
                         "Primary Contact")

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_update_a_client(self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.add",
                                            "first": "name",
                                            "last": "lastname",
                                            "email": "*****@*****.**",
                                            "uber_login": "******",
                                            "uber_pass": "******"
                                        }),
                                 content="1")

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.update",
                                            "client_id": "1",
                                            "first": "name-updated",
                                            "last": "lastname-updated",
                                            "email":
                                            "*****@*****.**",
                                            "uber_login": "******"
                                        }),
                                 content=True)

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.get",
                                            "client_id": "1"
                                        }),
                                 content={
                                     "clientid":
                                     "1",
                                     "first":
                                     "name-updated",
                                     "last":
                                     "lastname-updated",
                                     "email":
                                     "*****@*****.**",
                                     "login":
                                     "******",
                                     "listed_company":
                                     "lastname-updated, name-updated"
                                 })

    def test_client_get_returns_successfully(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.add",
                              "first": "John",
                          })
            client_id = json.loads(resp.data.decode('utf-8')).get("data")

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.get",
                              "client_id": client_id
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "clientid": client_id,
                    "first": "John",
                    "listed_company": ", John"
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_client_get_returns_successfully_with_acls_returns_an_empty_list(
            self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.add",
                                            "first": "John"
                                        }),
                                 content="1")

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.get",
                                            "client_id": "1",
                                            "acls": "1"
                                        }),
                                 content={
                                     "clientid": "1",
                                     "first": "John",
                                     "acls": [],
                                     "listed_company": ", John"
                                 })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_client_get_returns_listed_company_that_is_the_company(
            self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.add",
                                            "company": "CompanyName"
                                        }),
                                 content="1")

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.get",
                                            "client_id": "1"
                                        }),
                                 content={
                                     "clientid": "1",
                                     "company": "CompanyName",
                                     "listed_company": "CompanyName"
                                 })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_client_get_returns_listed_company_that_is_first_and_last_when_no_company(
            self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.add",
                                            "first": "John",
                                            "last": "Smith",
                                            "company": ""
                                        }),
                                 content="1")

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.get",
                                            "client_id": "1"
                                        }),
                                 content={
                                     "clientid": "1",
                                     "company": "",
                                     "listed_company": "Smith, John",
                                     "first": "John",
                                     "last": "Smith"
                                 })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_contact_get_returns_listed_company_that_is_the_company(
            self, random_id_mock):
        random_id_mock.side_effect = [1, 2, 3]
        with self.app.test_client() as c:
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.add",
                                            "company": "CompanyName"
                                        }),
                                 content="1")
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.contact_add",
                                            "client_id": "1"
                                        }),
                                 content="3")

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.contact_get",
                                            "contact_id": "3"
                                        }),
                                 content={
                                     "contact_id": "3",
                                     "client_id": "1",
                                     "email_name": "",
                                     "email_domain": "",
                                     "password": "******",
                                     "password_timeout": "0",
                                     "password_changed": "1549657344",
                                     "first": "",
                                     "last": "",
                                     "listed_company": "CompanyName"
                                 })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_contact_get_returns_listed_company_that_is_first_and_last_when_no_company(
            self, random_id_mock):
        random_id_mock.side_effect = [1, 2, 3]
        with self.app.test_client() as c:
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.add",
                                            "first": "John",
                                            "last": "Smith",
                                            "company": ""
                                        }),
                                 content="1")
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.contact_add",
                                            "client_id": "1"
                                        }),
                                 content="3")

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.contact_get",
                                            "contact_id": "3"
                                        }),
                                 content={
                                     "contact_id": "3",
                                     "client_id": "1",
                                     "email_name": "",
                                     "email_domain": "",
                                     "password": "******",
                                     "password_timeout": "0",
                                     "password_changed": "1549657344",
                                     "first": "",
                                     "last": "",
                                     "listed_company": "Smith, John"
                                 })

    def test_client_get_with_user_login_returns_successfully(self):
        self.data_store.clients = [{"clientid": "1", "contact_id": '0'}]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.get",
                              "user_login": "******"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "clientid": "1",
                    "listed_company": ", "
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_client_get_errs_when_no_match(self):
        self.data_store.clients = [{"clientid": "100"}]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.get",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 1,
                "error_message": "Client ID '1' not found.",
                "status": False
            })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_client_contact_add_creates_a_contact(self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.contact_add",
                                            "client_id": "12345",
                                            "real_name": "John smith",
                                            "description": "Describe me",
                                            "phone": "Mine phone",
                                            "email": "*****@*****.**",
                                            "login": "******",
                                            "password": "******"
                                        }),
                                 content="1")

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.contact_get",
                                            "contact_id": "1"
                                        }),
                                 content={
                                     "contact_id": "1",
                                     "client_id": "12345",
                                     "real_name": "John smith",
                                     "email_name": "john.smith",
                                     "email_domain": "invalid.com",
                                     "login": "******",
                                     "password": "******",
                                     "password_timeout": "0",
                                     "password_changed": "1549657344",
                                     "description": "Describe me",
                                     "phone": "Mine phone",
                                     "email": "*****@*****.**",
                                     "first": "John smith",
                                     "last": ""
                                 })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_client_contact_get_by_user_login(self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.contact_add",
                                            "client_id": "12345",
                                            "real_name": "John smith",
                                            "description": "Describe me",
                                            "phone": "Mine phone",
                                            "email": "*****@*****.**",
                                            "login": "******",
                                            "password": "******"
                                        }),
                                 content="1")

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.contact_get",
                                            "user_login": "******"
                                        }),
                                 content={
                                     "contact_id": "1",
                                     "client_id": "12345",
                                     "real_name": "John smith",
                                     "email_name": "john.smith",
                                     "email_domain": "invalid.com",
                                     "login": "******",
                                     "password": "******",
                                     "password_timeout": "0",
                                     "password_changed": "1549657344",
                                     "description": "Describe me",
                                     "phone": "Mine phone",
                                     "email": "*****@*****.**",
                                     "first": "John smith",
                                     "last": ""
                                 })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_client_contact_update(self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.contact_add",
                                            "client_id": "12345",
                                            "real_name": "John smith",
                                            "description": "Describe me",
                                            "phone": "Mine phone",
                                            "email": "*****@*****.**",
                                            "login": "******",
                                            "password": "******"
                                        }),
                                 content="1")

            self._assert_success(c.post(
                'api/2.0/',
                data={
                    "method": "client.contact_update",
                    "contact_id": "1",
                    "real_name": "John smith-UPDATED",
                    "description": "Describe me-UPDATED",
                    "phone": "Mine phone-UPDATED",
                    "email": "[email protected]",
                    "login": "******",
                    "password": "******"
                }),
                                 content=True)

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.contact_get",
                                            "contact_id": "1"
                                        }),
                                 content={
                                     "contact_id": "1",
                                     "client_id": "12345",
                                     "real_name": "John smith-UPDATED",
                                     "email_name": "john.smith-UPDATED",
                                     "email_domain": "invalid.com-UPDATED",
                                     "login": "******",
                                     "password": "******",
                                     "password_timeout": "0",
                                     "password_changed": "1549657344",
                                     "description": "Describe me-UPDATED",
                                     "phone": "Mine phone-UPDATED",
                                     "email":
                                     "[email protected]",
                                     "first": "John smith-UPDATED",
                                     "last": ""
                                 })

    def test_client_contact_get_returns_error_when_empty_payload_provided(
            self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/', data={
                "method": "client.contact_get",
            })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 1,
                "error_message": "No contact ID specified",
                "status": False
            })

    def test_client_contact_get_with_bad_contact_id_returns_error(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.contact_get",
                              "contact_id": "bad"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 1,
                "error_message": "Invalid contact_id specified.",
                "status": False
            })

    def test_client_contact_get_with_bad_user_login_returns_error(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.contact_get",
                              "user_login": "******"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 1,
                "error_message": "Invalid user_login specified.",
                "status": False
            })

    def test_client_contact_list_returns_contacts_for_given_client_id(self):
        contact_1 = {
            "contact_id": '1',
            "client_id": '100',
            "real_name": "John Patate",
            "email": "*****@*****.**"
        }
        contact_2 = {
            "contact_id": '1',
            "client_id": '101',
            "real_name": "The Dude",
            "email": "*****@*****.**"
        }
        contact_3 = {
            "contact_id": '2',
            "client_id": '100',
            "real_name": "Joe Poutine",
            "email": "*****@*****.**"
        }

        self.data_store.contacts.extend([contact_1, contact_2, contact_3])

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.contact_list",
                              "client_id": "100",
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    '1': contact_1,
                    '2': contact_3
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_client_contact_list_with_bad_client_id_returns_error(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.contact_list",
                              "client_id": "does_not_exist"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 1,
                "error_message": "Invalid client_id specified.",
                "status": False
            })

    def test_client_cc_add_is_successful(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_add",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.data.decode('utf-8')), {
            "data": 1,
            "error_code": None,
            "error_message": "",
            "status": True
        })

    def test_client_cc_add_fails_returns_error(self):
        self.client.credit_card_response = FakeUbersmithError(999, 'oh fail')

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_add",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 999,
                "error_message": "oh fail",
                "status": False
            })

    def test_client_cc_update_is_successful(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_update",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.data.decode('utf-8')), {
            "data": True,
            "error_code": None,
            "error_message": "",
            "status": True
        })

    def test_client_cc_update_fails_returns_error(self):
        self.client.credit_card_response = FakeUbersmithError(999, 'oh fail')

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_update",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 999,
                "error_message": "oh fail",
                "status": False
            })

    def test_client_cc_info_with_billing_info_id(self):
        self.data_store.credit_cards = [{"billing_info_id": "123"}]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_info",
                              "billing_info_id": "123"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "123": {
                        "billing_info_id": "123"
                    }
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_client_cc_info_with_client_id(self):
        self.data_store.credit_cards = [{
            "clientid": "1",
            "billing_info_id": "123"
        }]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_info",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "123": {
                        "billing_info_id": "123",
                        "clientid": "1"
                    }
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_client_cc_info_fails(self):
        self.data_store.credit_cards = [{
            "clientid": "1",
            "billing_info_id": "123"
        }]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_info",
                              "bogus": "thing"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 1,
                "error_message":
                "request failed: client_id parameter not supplied",
                "status": False
            })

    def test_client_cc_delete_is_successful(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_delete",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.data.decode('utf-8')), {
            "data": True,
            "error_code": None,
            "error_message": "",
            "status": True
        })

    def test_client_cc_delete_fails(self):
        self.client.credit_card_delete_response = FakeUbersmithError(
            999, 'oh fail')

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_delete",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 999,
                "error_message": "oh fail",
                "status": False
            })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_client_contact_permission_set_and_list_one_negative_contact_permissions(
            self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            c.post('api/2.0/',
                   data={
                       "method": "client.contact_add",
                       "client_id": "12345",
                       "real_name": "John smith",
                       "description": "Describe me",
                       "phone": "Mine phone",
                       "email": "*****@*****.**",
                       "login": "******",
                       "password": "******"
                   })

            c.post('api/2.0/',
                   data={
                       "method": "client.contact_permission_set",
                       "contact_id": "1",
                       "resource_name": "client.manage_contacts",
                       "action": "create",
                       "type": "deny"
                   })

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method":
                                            "client.contact_permission_list",
                                            "contact_id": "1",
                                            "resource_name":
                                            "client.manage_contacts",
                                            "effective": "1"
                                        }),
                                 content={
                                     '123': {
                                         'action': [],
                                         'actions': ['2', '1', '3', '4'],
                                         'active': '1',
                                         'effective': {
                                             'create': False,
                                             'delete': 0,
                                             'read': 0,
                                             'update': 0
                                         },
                                         'label': 'Manage Contacts',
                                         'lft': '',
                                         'name': 'client.manage_contacts',
                                         'parent_id': '',
                                         'resource_id': '123',
                                         'rgt': ''
                                     }
                                 })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_client_contact_permission_set_and_list_one_positive_contact_permissions(
            self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            c.post('api/2.0/',
                   data={
                       "method": "client.contact_add",
                       "client_id": "12345",
                       "real_name": "John smith",
                       "description": "Describe me",
                       "phone": "Mine phone",
                       "email": "*****@*****.**",
                       "login": "******",
                       "password": "******"
                   })

            c.post('api/2.0/',
                   data={
                       "method": "client.contact_permission_set",
                       "contact_id": "1",
                       "resource_name": "client.manage_contacts",
                       "action": "create",
                       "type": "allow"
                   })

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method":
                                            "client.contact_permission_list",
                                            "contact_id": "1",
                                            "resource_name":
                                            "client.manage_contacts",
                                            "effective": "1"
                                        }),
                                 content={
                                     '123': {
                                         'action': [],
                                         'actions': ['2', '1', '3', '4'],
                                         'active': '1',
                                         'effective': {
                                             'create': 1,
                                             'delete': 0,
                                             'read': 0,
                                             'update': 0
                                         },
                                         'label': 'Manage Contacts',
                                         'lft': '',
                                         'name': 'client.manage_contacts',
                                         'parent_id': '',
                                         'resource_id': '123',
                                         'rgt': ''
                                     }
                                 })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_client_contact_permission_set_and_list_all_negative_contact_permissions(
            self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            c.post('api/2.0/',
                   data={
                       "method": "client.contact_add",
                       "client_id": "12345",
                       "real_name": "John smith",
                       "description": "Describe me",
                       "phone": "Mine phone",
                       "email": "*****@*****.**",
                       "login": "******",
                       "password": "******"
                   })

            c.post('api/2.0/',
                   data={
                       "method": "client.contact_permission_set",
                       "contact_id": "1",
                       "resource_name": "client.manage_contacts",
                       "action": "create",
                       "type": "deny"
                   })
            c.post('api/2.0/',
                   data={
                       "method": "client.contact_permission_set",
                       "contact_id": "1",
                       "resource_name": "client.manage_contacts",
                       "action": "read",
                       "type": "deny"
                   })
            c.post('api/2.0/',
                   data={
                       "method": "client.contact_permission_set",
                       "contact_id": "1",
                       "resource_name": "client.manage_contacts",
                       "action": "update",
                       "type": "deny"
                   })
            c.post('api/2.0/',
                   data={
                       "method": "client.contact_permission_set",
                       "contact_id": "1",
                       "resource_name": "client.manage_contacts",
                       "action": "delete",
                       "type": "deny"
                   })

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method":
                                            "client.contact_permission_list",
                                            "contact_id": "1",
                                            "resource_name":
                                            "client.manage_contacts",
                                            "effective": "1"
                                        }),
                                 content={
                                     '123': {
                                         'action': [],
                                         'actions': ['2', '1', '3', '4'],
                                         'active': '1',
                                         'effective': {
                                             'create': False,
                                             'delete': False,
                                             'read': False,
                                             'update': False
                                         },
                                         'label': 'Manage Contacts',
                                         'lft': '',
                                         'name': 'client.manage_contacts',
                                         'parent_id': '',
                                         'resource_id': '123',
                                         'rgt': ''
                                     }
                                 })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_client_contact_permission_set_and_list_all_positive_contact_permissions(
            self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            c.post('api/2.0/',
                   data={
                       "method": "client.contact_add",
                       "client_id": "12345",
                       "real_name": "John smith",
                       "description": "Describe me",
                       "phone": "Mine phone",
                       "email": "*****@*****.**",
                       "login": "******",
                       "password": "******"
                   })

            c.post('api/2.0/',
                   data={
                       "method": "client.contact_permission_set",
                       "contact_id": "1",
                       "resource_name": "client.manage_contacts",
                       "action": "create",
                       "type": "allow"
                   })
            c.post('api/2.0/',
                   data={
                       "method": "client.contact_permission_set",
                       "contact_id": "1",
                       "resource_name": "client.manage_contacts",
                       "action": "read",
                       "type": "allow"
                   })
            c.post('api/2.0/',
                   data={
                       "method": "client.contact_permission_set",
                       "contact_id": "1",
                       "resource_name": "client.manage_contacts",
                       "action": "update",
                       "type": "allow"
                   })
            c.post('api/2.0/',
                   data={
                       "method": "client.contact_permission_set",
                       "contact_id": "1",
                       "resource_name": "client.manage_contacts",
                       "action": "delete",
                       "type": "allow"
                   })

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method":
                                            "client.contact_permission_list",
                                            "contact_id": "1",
                                            "resource_name":
                                            "client.manage_contacts",
                                            "effective": "1"
                                        }),
                                 content={
                                     '123': {
                                         'action': [],
                                         'actions': ['2', '1', '3', '4'],
                                         'active': '1',
                                         'effective': {
                                             'create': 1,
                                             'delete': 1,
                                             'read': 1,
                                             'update': 1
                                         },
                                         'label': 'Manage Contacts',
                                         'lft': '',
                                         'name': 'client.manage_contacts',
                                         'parent_id': '',
                                         'resource_id': '123',
                                         'rgt': ''
                                     }
                                 })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_client_contact_permission_set_and_list_mixed_contact_permissions(
            self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            c.post('api/2.0/',
                   data={
                       "method": "client.contact_add",
                       "client_id": "12345",
                       "real_name": "John smith",
                       "description": "Describe me",
                       "phone": "Mine phone",
                       "email": "*****@*****.**",
                       "login": "******",
                       "password": "******"
                   })

            c.post('api/2.0/',
                   data={
                       "method": "client.contact_permission_set",
                       "contact_id": "1",
                       "resource_name": "client.manage_contacts",
                       "action": "create",
                       "type": "allow"
                   })
            c.post('api/2.0/',
                   data={
                       "method": "client.contact_permission_set",
                       "contact_id": "1",
                       "resource_name": "client.manage_contacts",
                       "action": "read",
                       "type": "deny"
                   })

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method":
                                            "client.contact_permission_list",
                                            "contact_id": "1",
                                            "resource_name":
                                            "client.manage_contacts",
                                            "effective": "1"
                                        }),
                                 content={
                                     '123': {
                                         'action': [],
                                         'actions': ['2', '1', '3', '4'],
                                         'active': '1',
                                         'effective': {
                                             'create': 1,
                                             'delete': False,
                                             'read': 0,
                                             'update': 0
                                         },
                                         'label': 'Manage Contacts',
                                         'lft': '',
                                         'name': 'client.manage_contacts',
                                         'parent_id': '',
                                         'resource_id': '123',
                                         'rgt': ''
                                     }
                                 })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_client_contact_permission_list_without_previous_permission_set_does_not_fail(
            self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            c.post('api/2.0/',
                   data={
                       "method": "client.contact_add",
                       "client_id": "12345",
                       "real_name": "John smith",
                       "description": "Describe me",
                       "phone": "Mine phone",
                       "email": "*****@*****.**",
                       "login": "******",
                       "password": "******"
                   })

            resp = c.post('api/2.0/',
                          data={
                              "method": "client.contact_permission_list",
                              "contact_id": "1",
                              "resource_name": "client.manage_contacts",
                              "effective": "1"
                          })
            self.assertEqual(resp.status_code, 200)
예제 #10
0
class TestClientModule(unittest.TestCase):
    def setUp(self):
        self.data_store = DataStore()
        self.client = Client(self.data_store)

        self.app = Flask(__name__)
        self.base_uber_api = UbersmithBase(self.data_store)

        self.client.hook_to(self.base_uber_api)
        self.base_uber_api.hook_to(self.app)

    def test_client_add_creates_a_client(self):

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.add",
                              "first": "John",
                              "last": "Smith",
                              "email": "*****@*****.**",
                              "uber_login": "******",
                              "uber_pass": "******"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.data.decode('utf-8')), {
            "data": "1",
            "error_code": None,
            "error_message": "",
            "status": True
        })
        self.assertEqual(self.data_store.clients[0]["login"], "john")

    def test_client_get_returns_successfully(self):
        with self.app.test_client() as c:
            c.post('api/2.0/',
                   data={
                       "method": "client.add",
                       "first": "John",
                   })

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.get",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "clientid": "1",
                    "first": "John"
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_client_get_with_user_login_returns_successfully(self):
        self.data_store.clients = [{"clientid": "1", "contact_id": '0'}]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.get",
                              "user_login": "******"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "clientid": "1"
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_client_get_errs_when_no_match(self):
        self.data_store.clients = [{"clientid": "100"}]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.get",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 1,
                "error_message": "Client ID '1' not found.",
                "status": False
            })

    def test_client_contact_add_creates_a_contact(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.contact_add",
                              "first": "John",
                              "last": "Smith",
                              "email": "*****@*****.**",
                              "uber_login": "******",
                              "uber_pass": "******"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.data.decode('utf-8')), {
            "data": "1",
            "error_code": None,
            "error_message": "",
            "status": True
        })

    def test_client_contact_get_returns_error_when_empty_payload_provided(
            self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/', data={
                "method": "client.contact_get",
            })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 1,
                "error_message": "No contact ID specified",
                "status": False
            })

    def test_client_contact_get_with_bad_contact_id_returns_error(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.contact_get",
                              "contact_id": "bad"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 1,
                "error_message": "Invalid contact_id specified.",
                "status": False
            })

    def test_client_contact_get_with_bad_user_login_returns_error(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.contact_get",
                              "user_login": "******"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 1,
                "error_message": "Invalid user_login specified.",
                "status": False
            })

    def test_client_contact_get_with_contact_id_returns_a_contact(self):
        a_contact = {"contact_id": "100"}

        self.data_store.contacts.append(a_contact)

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.contact_get",
                              "contact_id": "100",
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": a_contact,
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_client_contact_get_with_user_login_returns_a_contact(self):
        a_contact = {
            "contact_id": "100",
            "first": "John",
            "last": "Smith",
            "email": "*****@*****.**",
            "uber_login": "******",
        }

        self.data_store.contacts.append(a_contact)

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.contact_get",
                              "user_login": "******",
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": a_contact,
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_client_cc_add_is_successful(self):

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_add",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.data.decode('utf-8')), {
            "data": 1,
            "error_code": None,
            "error_message": "",
            "status": True
        })

    def test_client_cc_add_fails_returns_error(self):
        self.client.credit_card_response = FakeUbersmithError(999, 'oh fail')

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_add",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 999,
                "error_message": "oh fail",
                "status": False
            })

    def test_client_cc_update_is_successful(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_update",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.data.decode('utf-8')), {
            "data": True,
            "error_code": None,
            "error_message": "",
            "status": True
        })

    def test_client_cc_update_fails_returns_error(self):
        self.client.credit_card_response = FakeUbersmithError(999, 'oh fail')

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_update",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 999,
                "error_message": "oh fail",
                "status": False
            })

    def test_client_cc_info_with_billing_info_id(self):
        self.data_store.credit_cards = [{"billing_info_id": "123"}]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_info",
                              "billing_info_id": "123"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "123": {
                        "billing_info_id": "123"
                    }
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_client_cc_info_with_client_id(self):
        self.data_store.credit_cards = [{
            "clientid": "1",
            "billing_info_id": "123"
        }]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_info",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "123": {
                        "billing_info_id": "123",
                        "clientid": "1"
                    }
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_client_cc_info_fails(self):
        self.data_store.credit_cards = [{
            "clientid": "1",
            "billing_info_id": "123"
        }]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_info",
                              "bogus": "thing"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 1,
                "error_message":
                "request failed: client_id parameter not supplied",
                "status": False
            })

    def test_client_cc_delete_is_successful(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_delete",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(json.loads(resp.data.decode('utf-8')), {
            "data": True,
            "error_code": None,
            "error_message": "",
            "status": True
        })

    def test_client_cc_delete_fails(self):
        self.client.credit_card_delete_response = FakeUbersmithError(
            999, 'oh fail')

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "client.cc_delete",
                              "client_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 999,
                "error_message": "oh fail",
                "status": False
            })
예제 #11
0
class TestUberModule(ApiTestBase):
    def setUp(self):
        self.data_store = DataStore()
        self.uber = Uber(self.data_store)
        self.client = Client(self.data_store)

        self.app = Flask(__name__)
        self.base_uber_api = UbersmithBase(self.data_store)

        self.uber.hook_to(self.base_uber_api)
        self.client.hook_to(self.base_uber_api)
        self.base_uber_api.hook_to(self.app)

    def test_service_plan_get_returns_if_service_plan_found(self):
        self.data_store.service_plans = [{"plan_id": "1"}]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.service_plan_get",
                              "plan_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "plan_id": "1"
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_service_plan_get_errs_if_service_plan_not_found(self):
        self.data_store.service_plans = [{"plan_id": 1}]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.service_plan_get",
                              "plan_id": "100"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 3,
                "error_message": "No Service Plan found",
                "status": False
            })

    def test_service_plan_get_errs_with_service_plan_error(self):
        self.uber.service_plan_error = FakeUbersmithError(999, 'some error')

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.service_plan_get",
                              "plan_id": "100"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 999,
                "error_message": "some error",
                "status": False
            })

    def test_service_plan_list_returns_all_plans(self):
        self.data_store.service_plans_list = {
            "1": {
                "code": "42"
            },
            "2": {
                "code": "99"
            }
        }

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={"method": "uber.service_plan_list"})

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "1": {
                        "code": "42"
                    },
                    "2": {
                        "code": "99"
                    }
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_service_plan_list_returns_plans_matching_code(self):
        self.data_store.service_plans_list = {
            "1": {
                "plan_id": "1",
                "code": "42"
            },
            "2": {
                "plan_id": "1",
                "code": "99"
            }
        }

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.service_plan_list",
                              'code': '42'
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "1": {
                        "code": "42",
                        "plan_id": "1"
                    }
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_check_login_succesfully_for_client(self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.add",
                                            "first": "name",
                                            "last": "lastname",
                                            "email": "*****@*****.**",
                                            "uber_login": "******",
                                            "uber_pass": "******"
                                        }),
                                 content="1")

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "uber.check_login",
                                            "login": "******",
                                            "pass": "******"
                                        }),
                                 content={
                                     "id": "1",
                                     "login": "******",
                                     "client_id": "1",
                                     "contact_id": 0,
                                     "fullname": "name lastname",
                                     "email": "*****@*****.**",
                                     "last_login": None,
                                     "password_timeout": "0",
                                     "password_changed": "1549380089",
                                     "type": "client"
                                 })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_check_login_succesfully_for_client_bare_info(
            self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.add",
                                            "uber_login": "******",
                                            "uber_pass": "******"
                                        }),
                                 content="1")

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "uber.check_login",
                                            "login": "******",
                                            "pass": "******"
                                        }),
                                 content={
                                     "id": "1",
                                     "login": "******",
                                     "client_id": "1",
                                     "contact_id": 0,
                                     "fullname": "",
                                     "email": "",
                                     "last_login": None,
                                     "password_timeout": "0",
                                     "password_changed": "1549380089",
                                     "type": "client"
                                 })

    def test_check_login_failed(self):
        self.data_store.clients = [{
            'clientid': '1',
            'first': 'John',
            'last': 'Smith',
            'email': '*****@*****.**',
            'login': '******',
            'uber_pass': '******',
        }]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.check_login",
                              "login": "******",
                              "pass": "******"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 3,
                "error_message": "Invalid login or password.",
                "status": False
            })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_check_login_successfully_for_contact(self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.contact_add",
                                            "client_id": "12345",
                                            "real_name": "my real name",
                                            "email": "*****@*****.**",
                                            "login": "******",
                                            "password": "******"
                                        }),
                                 content="1")

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "uber.check_login",
                                            "login": "******",
                                            "pass": "******"
                                        }),
                                 content={
                                     "id": "12345-1",
                                     "login": "******",
                                     "client_id": "12345",
                                     "contact_id": "1",
                                     "fullname": "my real name",
                                     "email": "*****@*****.**",
                                     "last_login": None,
                                     "password_timeout": "0",
                                     "password_changed": "1549380089",
                                     "type": "contact"
                                 })

    @mock.patch("fake_ubersmith.api.methods.client.a_random_id")
    def test_check_login_successfully_for_contact_bare_info(
            self, random_id_mock):
        random_id_mock.return_value = 1
        with self.app.test_client() as c:
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "client.contact_add",
                                            "client_id": "12345",
                                            "login": "******",
                                            "password": "******"
                                        }),
                                 content="1")

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "uber.check_login",
                                            "login": "******",
                                            "pass": "******"
                                        }),
                                 content={
                                     "id": "12345-1",
                                     "login": "******",
                                     "client_id": "12345",
                                     "contact_id": "1",
                                     "fullname": "",
                                     "email": "@",
                                     "last_login": None,
                                     "password_timeout": "0",
                                     "password_changed": "1549380089",
                                     "type": "contact"
                                 })

    def test_get_admin_roles_when_passing_valid_user_id_and_role_id(self):
        role_id = "some_role_id"
        self.data_store.roles = {
            role_id: {
                'role_id': role_id,
                'name': 'A Admin Role',
                'descr': 'A Admin Role'
            }
        }
        self.data_store.user_mapping = {"some_user_id": {"roles": {role_id}}}

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.acl_admin_role_get",
                              "role_id": role_id,
                              "userid": "some_user_id",
                          })

        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "status": True,
                "error_code": None,
                "error_message": "",
                "data": {
                    role_id: {
                        "role_id": role_id,
                        'name': 'A Admin Role',
                        'descr': 'A Admin Role'
                    }
                }
            })

    def test_get_admin_roles_when_passing_bad_role_id(self):
        role_id = "some_role_id"
        self.data_store.roles = {
            role_id: {
                'name': 'Some event type',
                'descr': 'client',
                'acls': {
                    'admin.portal': {
                        'read': '1'
                    }
                }
            }
        }
        self.data_store.user_mapping = {
            "some_user_id": {
                "role_id": "some_role_id"
            }
        }

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.acl_admin_role_get",
                              "role_id": "bogus_id_that_won't_match"
                          })

        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "error_code": 1,
                "error_message": "No User Roles found",
                "status": False,
                "data": ""
            })

    def test_get_admin_roles_successfully_when_passing_valid_role_id(self):
        role_id = "some_role_id"
        self.data_store.roles = {
            role_id: {
                'name': 'A Admin Role',
                'descr': 'A Admin Role',
                'acls': {
                    'admin.portal': {
                        'read': '1'
                    }
                }
            }
        }

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.acl_admin_role_get",
                              "role_id": role_id
                          })

        self.assertEqual(
            json.loads(resp.data.decode('utf-8')).get('data'), {
                'name': 'A Admin Role',
                'descr': 'A Admin Role',
                'acls': {
                    'admin.portal': {
                        'read': '1'
                    }
                }
            })

    def test_get_admin_roles_fails_when_no_role_id_passed(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.acl_admin_role_get",
                              "userid": "some_user_id"
                          })

        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "error_code": 1,
                "error_message": "role_id parameter not specified",
                "status": False,
                "data": ""
            })

    def test_get_admin_roles_fails_when_no_role_could_be_found_for_user(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.acl_admin_role_get",
                              "userid": "some_user_id",
                              "role_id": "some_role_id"
                          })

        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "error_code": 1,
                "error_message": "No User Roles found",
                "status": False,
                "data": ""
            })

    def test_get_admin_roles_fails_when_user_not_found(self):
        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.acl_admin_role_get",
                              "userid": "some_user_id",
                              "role_id": "some_role_id"
                          })

        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "error_code": 1,
                "error_message": "No User Roles found",
                "status": False,
                "data": ""
            })

    def test_acl_resource_add(self):
        self.maxDiff = None
        with self.app.test_client() as c:
            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "uber.acl_resource_add",
                                            "parent_resource_name": "",
                                            "resource_name": "my.resource",
                                            "label": "my label",
                                            "actions":
                                            "create,read,update,delete"
                                        }),
                                 content="")

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "uber.acl_resource_add",
                                            "parent_resource_name":
                                            "my.resource",
                                            "resource_name": "my.child1",
                                            "label": "my label 2",
                                            "actions": "read"
                                        }),
                                 content="")

            self._assert_success(c.post('api/2.0/',
                                        data={
                                            "method": "uber.acl_resource_add",
                                            "parent_resource_name":
                                            "my.child1",
                                            "resource_name": "my.child2",
                                            "label": "my label 3"
                                        }),
                                 content="")

            self._assert_success(c.post(
                'api/2.0/', data={"method": "uber.acl_resource_list"}),
                                 content={
                                     "1": {
                                         "resource_id": "1",
                                         "name": "my.resource",
                                         "parent_id": "0",
                                         "lft": "0",
                                         "rgt": "0",
                                         "active": "1",
                                         "label": "my label",
                                         "actions": {
                                             "1": "Create",
                                             "2": "View",
                                             "3": "Update",
                                             "4": "Delete"
                                         },
                                         "children": {
                                             "2": {
                                                 "resource_id": "2",
                                                 "name": "my.child1",
                                                 "parent_id": "1",
                                                 "lft": "0",
                                                 "rgt": "0",
                                                 "active": "1",
                                                 "label": "my label 2",
                                                 "actions": {
                                                     "2": "View",
                                                 },
                                                 "children": {
                                                     "3": {
                                                         "resource_id": "3",
                                                         "name": "my.child2",
                                                         "parent_id": "2",
                                                         "lft": "0",
                                                         "rgt": "0",
                                                         "active": "1",
                                                         "label": "my label 3",
                                                         "actions": {
                                                             "1": "Create",
                                                             "2": "View",
                                                             "3": "Update",
                                                             "4": "Delete"
                                                         },
                                                         "children": []
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 })

    def test_acl_resource_add_error(self):
        with self.app.test_client() as c:
            self._assert_error(c.post('api/2.0/',
                                      data={
                                          "method": "uber.acl_resource_add",
                                          "parent_resource_name": "baaaaaaah",
                                          "resource_name": "my.resource"
                                      }),
                               code=1,
                               message="Resource [baaaaaaah] not found",
                               content="")
예제 #12
0
    def setUp(self):
        self.app = Flask(__name__)
        data_store = DataStore()

        self.ubersmith_base = UbersmithBase(data_store)
        self.ubersmith_base.hook_to(self.app)
예제 #13
0
class TestUberModule(unittest.TestCase):
    def setUp(self):
        self.data_store = DataStore()
        self.uber = Uber(self.data_store)

        self.app = Flask(__name__)
        self.base_uber_api = UbersmithBase(self.data_store)

        self.uber.hook_to(self.base_uber_api)
        self.base_uber_api.hook_to(self.app)

    def test_service_plan_get_returns_if_service_plan_found(self):
        self.data_store.service_plans = [{"plan_id": "1"}]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.service_plan_get",
                              "plan_id": "1"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "plan_id": "1"
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_service_plan_get_errs_if_service_plan_not_found(self):
        self.data_store.service_plans = [{"plan_id": 1}]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.service_plan_get",
                              "plan_id": "100"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 3,
                "error_message": "No Service Plan found",
                "status": False
            })

    def test_service_plan_get_errs_with_service_plan_error(self):
        self.uber.service_plan_error = FakeUbersmithError(999, 'some error')

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.service_plan_get",
                              "plan_id": "100"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 999,
                "error_message": "some error",
                "status": False
            })

    def test_service_plan_list_returns_all_plans(self):

        self.data_store.service_plans_list = {
            "1": {
                "code": "42"
            },
            "2": {
                "code": "99"
            }
        }

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={"method": "uber.service_plan_list"})

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "1": {
                        "code": "42"
                    },
                    "2": {
                        "code": "99"
                    }
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_service_plan_list_returns_plans_matching_code(self):
        self.data_store.service_plans_list = {
            "1": {
                "plan_id": "1",
                "code": "42"
            },
            "2": {
                "plan_id": "1",
                "code": "99"
            }
        }

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.service_plan_list",
                              'code': '42'
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "1": {
                        "code": "42",
                        "plan_id": "1"
                    }
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_check_login_succesfully_for_client(self):
        self.data_store.clients = [{
            'clientid': '1',
            'contact_id': '0',
            'first': 'John',
            'last': 'Smith',
            'email': '*****@*****.**',
            'login': '******',
            'uber_pass': '******',
        }]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.check_login",
                              "login": "******",
                              "pass": "******"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "client_id": "1",
                    "contact_id": 0,
                    "type": "client",
                    "login": "******",
                    "email": "*****@*****.**",
                    "fullname": "John Smith"
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })

    def test_check_login_failed(self):
        self.data_store.clients = [{
            'clientid': '1',
            'first': 'John',
            'last': 'Smith',
            'email': '*****@*****.**',
            'login': '******',
            'uber_pass': '******',
        }]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.check_login",
                              "login": "******",
                              "pass": "******"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": "",
                "error_code": 3,
                "error_message": "Invalid login or password.",
                "status": False
            })

    def test_check_login_sucessfully_for_contact(self):
        self.data_store.contacts = [{
            'client_id': '1234',
            'contact_id': '1',
            'real_name': 'Line Doe',
            'email': '*****@*****.**',
            'login': '******',
            'password': '******',
        }]

        with self.app.test_client() as c:
            resp = c.post('api/2.0/',
                          data={
                              "method": "uber.check_login",
                              "login": "******",
                              "pass": "******"
                          })

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            json.loads(resp.data.decode('utf-8')), {
                "data": {
                    "client_id": "1234",
                    "contact_id": "1",
                    "type": "client",
                    "login": "******",
                    "email": "*****@*****.**",
                    "fullname": "Line Doe"
                },
                "error_code": None,
                "error_message": "",
                "status": True
            })