def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.api_object = EWSApi(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) for product in Prod.by_domain(TEST_DOMAIN): product.delete()
def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.api_object = EWSApi(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) self.api_object.prepare_commtrack_config() self.api_object.create_or_edit_roles() for user in CommCareUser.by_domain(TEST_DOMAIN): user.delete() for verified_number in VerifiedNumber.by_domain(TEST_DOMAIN): verified_number.delete()
def fix_users_with_more_than_one_phone_number(domain): config = EWSGhanaConfig.for_domain(domain) endpoint = GhanaEndpoint.from_config(config) ews_api = EWSApi(domain, endpoint) offset = 0 _, smsusers = endpoint.get_smsusers(filters={'with_more_than_one_number': True}) while smsusers: for sms_user in smsusers: ews_api.sms_user_sync(sms_user) offset += 100 _, smsusers = endpoint.get_smsusers(filters={'with_more_than_one_number': True}, offset=offset)
def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.stock_api_object = MockEWSStockDataSynchronization(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) self.api_object = EWSApi(TEST_DOMAIN, self.endpoint) self.api_object.prepare_commtrack_config() config = EWSGhanaConfig() config.domain = TEST_DOMAIN config.enabled = True config.all_stock_data = True config.password = '******' config.username = '******' config.url = 'http://test-api.com/' config.save() l1 = Location( name='Test location 1', external_id='3445', location_type='Hospital', domain=TEST_DOMAIN ) l2 = Location( name='Test location 2', external_id='4407', location_type='Hospital', domain=TEST_DOMAIN ) l1.save() l2.save() with open(os.path.join(self.datapath, 'sample_products.json')) as f: for product_json in json.loads(f.read()): self.api_object.product_sync(Product(product_json))
def migration_task(): for config in EWSGhanaConfig.get_all_steady_sync_configs(): if config.enabled: endpoint = GhanaEndpoint.from_config(config) ews_bootstrap_domain(EWSApi(config.domain, endpoint)) stock_data_task.delay( EWSStockDataSynchronization(config.domain, endpoint))
def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.api_object = EWSApi(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) self.api_object.prepare_commtrack_config() self.api_object.create_or_edit_roles() for user in WebUser.by_domain(TEST_DOMAIN): user.delete()
def setUpClass(cls): cls.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') cls.api_object = EWSApi(TEST_DOMAIN, cls.endpoint) cls.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) bootstrap_products(TEST_DOMAIN) cls.api_object.prepare_commtrack_config() with open(os.path.join(cls.datapath, 'sample_products.json')) as f: for p in json.loads(f.read()): cls.api_object.product_sync(Product(p))
def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.api_object = EWSApi(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) self.api_object.prepare_commtrack_config() for location in CouchLocation.by_domain(TEST_DOMAIN): location.delete() for sql_location in SQLLocation.objects.all(): sql_location.delete()
def setUp(self): self.endpoint = MockEndpoint("http://test-api.com/", "dummy", "dummy") self.api_object = EWSApi(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), "data") initial_bootstrap(TEST_DOMAIN) self.api_object.prepare_commtrack_config() self.api_object.create_or_edit_roles() for user in CommCareUser.by_domain(TEST_DOMAIN): user.delete() for verified_number in VerifiedNumber.by_domain(TEST_DOMAIN): verified_number.delete()
def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.stock_api_object = MockEWSStockDataSynchronization( TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) self.api_object = EWSApi(TEST_DOMAIN, self.endpoint) self.api_object.prepare_commtrack_config() config = EWSGhanaConfig() config.domain = TEST_DOMAIN config.enabled = True config.all_stock_data = True config.password = '******' config.username = '******' config.url = 'http://test-api.com/' config.save() l1 = Location(name='Test location 1', external_id='3445', location_type='Hospital', domain=TEST_DOMAIN) l2 = Location(name='Test location 2', external_id='4407', location_type='Hospital', domain=TEST_DOMAIN) l1.save() l2.save() SupplyPointCase.create_from_location(TEST_DOMAIN, l1) SupplyPointCase.create_from_location(TEST_DOMAIN, l2) l1.save() l2.save() with open(os.path.join(self.datapath, 'sample_products.json')) as f: for product_json in json.loads(f.read()): self.api_object.product_sync(Product(product_json))
class WebUsersSyncTest(TestCase): def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.api_object = EWSApi(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) self.api_object.prepare_commtrack_config() self.api_object.create_or_edit_roles() for user in WebUser.by_domain(TEST_DOMAIN): user.delete() def test_create_webuser(self): with open(os.path.join(self.datapath, 'sample_webusers.json')) as f: webuser = EWSUser(json.loads(f.read())[0]) self.assertEqual(0, len(WebUser.by_domain(TEST_DOMAIN))) ewsghana_webuser = self.api_object.web_user_sync(webuser) self.assertEqual(webuser.email, ewsghana_webuser.username) self.assertEqual(webuser.password, ewsghana_webuser.password) self.assertEqual(webuser.first_name, ewsghana_webuser.first_name) self.assertEqual(webuser.last_name, ewsghana_webuser.last_name) self.assertEqual(webuser.is_active, ewsghana_webuser.is_active) self.assertEqual(False, ewsghana_webuser.is_superuser) self.assertEqual(False, ewsghana_webuser.is_staff) domain_name = ewsghana_webuser.get_domains()[0] self.assertEqual(TEST_DOMAIN, domain_name) self.assertEqual(UserRole.get_read_only_role_by_domain(TEST_DOMAIN)._id, ewsghana_webuser.get_domain_membership(TEST_DOMAIN).role_id) def test_create_facility_manager(self): with open(os.path.join(self.datapath, 'sample_webusers.json')) as f: webuser = EWSUser(json.loads(f.read())[1]) self.assertEqual(0, len(WebUser.by_domain(TEST_DOMAIN))) self.api_object.web_user_sync(webuser) web_users = list(WebUser.by_domain(TEST_DOMAIN)) self.assertEqual(1, len(web_users)) self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN))) facility_manager_role = UserRole.by_domain_and_name(TEST_DOMAIN, 'Facility manager')[0] dm = web_users[0].get_domain_membership(TEST_DOMAIN) self.assertEqual(facility_manager_role.get_id, dm.role_id)
class ProductSyncTest(TestCase): def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.api_object = EWSApi(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) for product in Prod.by_domain(TEST_DOMAIN): product.delete() def test_create_product(self): with open(os.path.join(self.datapath, 'sample_products.json')) as f: product = Product(json.loads(f.read())[0]) self.assertEqual(0, len(Prod.by_domain(TEST_DOMAIN))) ewsghana_product = self.api_object.product_sync(product) self.assertEqual(product.sms_code, ewsghana_product.code.lower()) self.assertEqual(product.name, ewsghana_product.name) self.assertEqual(product.description, ewsghana_product.description) self.assertEqual(product.units, str(ewsghana_product.unit)) self.assertIsNotNone(ewsghana_product.program_id) program = Program.get(ewsghana_product.program_id) self.assertEqual(product.program.name, program.name) self.assertEqual(product.program.code, program.code)
class SMSUsersSyncTest(TestCase): def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.api_object = EWSApi(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) self.api_object.prepare_commtrack_config() self.api_object.create_or_edit_roles() for user in CommCareUser.by_domain(TEST_DOMAIN): user.delete() for verified_number in VerifiedNumber.by_domain(TEST_DOMAIN): verified_number.delete() def test_create_smsuser(self): with open(os.path.join(self.datapath, 'sample_smsusers.json')) as f: smsuser = SMSUser(json.loads(f.read())[0]) self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN))) ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) self.assertIsNotNone(ewsghana_smsuser.get_id) username_part = "%s%d" % (ewsghana_smsuser.name.strip().replace( ' ', '.').lower(), smsuser.id) username = "******" % (username_part, TEST_DOMAIN) self.assertEqual(username, ewsghana_smsuser.username) self.assertEqual(smsuser.is_active, str(ewsghana_smsuser.is_active)) self.assertEqual(False, ewsghana_smsuser.is_superuser) self.assertEqual(False, ewsghana_smsuser.is_staff) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) self.assertEqual(verified_number.phone_number, '2222222222') domain_name = ewsghana_smsuser.get_domains()[0] self.assertEqual(TEST_DOMAIN, domain_name) self.assertIsInstance(ewsghana_smsuser.user_data['role'], list) self.assertEqual(len(ewsghana_smsuser.user_data['role']), 1) self.assertEqual(ewsghana_smsuser.user_data['role'][0], 'Other') def test_edit_smsuser(self): with open(os.path.join(self.datapath, 'sample_smsusers.json')) as f: smsuser = SMSUser(json.loads(f.read())[0]) self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN))) ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) user_id = ewsghana_smsuser.get_id self.assertIsNotNone(user_id) smsuser.phone_numbers = ['111111111'] ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) self.assertEqual(user_id, ewsghana_smsuser.get_id) self.assertEqual(ewsghana_smsuser.default_phone_number, '111111111') self.assertListEqual(ewsghana_smsuser.phone_numbers, ['111111111']) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) self.assertEqual(verified_number.phone_number, '111111111') def test_edit_phone_number1(self): """ When phone number is deleted on EWS side it also should be deleted in HQ :return: """ with open(os.path.join(self.datapath, 'sample_smsusers.json')) as f: smsuser = SMSUser(json.loads(f.read())[0]) self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN))) ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) smsuser.phone_numbers = [] ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) self.assertIsNone(ewsghana_smsuser.default_phone_number) self.assertListEqual(ewsghana_smsuser.phone_numbers, []) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNone(verified_number) def test_edit_phone_number2(self): """ When phone number is added on EWS side it also should be added in HQ """ with open(os.path.join(self.datapath, 'sample_smsusers.json')) as f: smsuser = SMSUser(json.loads(f.read())[0]) smsuser.phone_numbers = [] self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN))) ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNone(verified_number) smsuser.phone_numbers = ['111111111'] ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) self.assertIsNotNone(ewsghana_smsuser.default_phone_number) self.assertListEqual(ewsghana_smsuser.phone_numbers, ['111111111']) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) self.assertEqual(verified_number.phone_number, '111111111') def test_edit_phone_number3(self): """ When phone number is edited on EWS side it also should be edited in HQ """ with open(os.path.join(self.datapath, 'sample_smsusers.json')) as f: smsuser = SMSUser(json.loads(f.read())[0]) self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN))) ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) self.assertEqual(verified_number.phone_number, '2222222222') smsuser.phone_numbers = ['111111111'] ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) self.assertIsNotNone(ewsghana_smsuser.default_phone_number) self.assertListEqual(ewsghana_smsuser.phone_numbers, ['111111111']) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) self.assertEqual(verified_number.phone_number, '111111111') def test_edit_phone_number4(self): """ Number shouldn't be changed when is not edited on EWS side. """ with open(os.path.join(self.datapath, 'sample_smsusers.json')) as f: smsuser = SMSUser(json.loads(f.read())[0]) self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN))) ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) self.assertEqual(verified_number.phone_number, '2222222222') ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) self.assertEqual(verified_number.phone_number, '2222222222')
def ews_bootstrap_domain_task(domain): ews_config = EWSGhanaConfig.for_domain(domain) return bootstrap_domain( EWSApi(domain, GhanaEndpoint.from_config(ews_config)))
def setUpClass(cls): cls.domain = initial_bootstrap(TEST_DOMAIN) db = get_db() if db.doc_exist(DOMAIN_MODULE_KEY): module_config = db.open_doc(DOMAIN_MODULE_KEY) module_map = module_config.get('module_map') if module_map: module_map[TEST_DOMAIN] = 'custom.ewsghana' else: module_config['module_map'][TEST_DOMAIN] = 'custom.ewsghana' else: module_config = db.save_doc({ '_id': DOMAIN_MODULE_KEY, 'module_map': { 'ewsghana-test-input-stock': 'custom.ewsghana' } }) db.save_doc(module_config) generator.instantiate_accounting_for_tests() account = BillingAccount.get_or_create_account_by_domain( cls.domain.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( cls.domain, edition=SoftwarePlanEdition.ENTERPRISE) subscription = Subscription.new_domain_subscription( account, cls.domain.name, plan) subscription.is_active = True subscription.save() cls.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') cls.api_object = EWSApi(TEST_DOMAIN, cls.endpoint) cls.api_object.prepare_commtrack_config() cls.api_object.prepare_custom_fields() cls.datapath = os.path.join(os.path.dirname(__file__), 'data') with open(os.path.join(cls.datapath, 'sample_products.json')) as f: for p in json.loads(f.read()): cls.api_object.product_sync(Product(p)) with open(os.path.join(cls.datapath, 'sample_locations.json')) as f: for loc in json.loads(f.read()): cls.api_object.location_sync(Location(loc)) cls.test_facility3 = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='tsactive') cls.testregion2 = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='testregion2') cls.rsp = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='rsp') cls.test_district = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='testdistrict') cls.username1 = 'ews_user1' cls.password1 = 'dummy' cls.web_user1 = WebUser.create(TEST_DOMAIN, cls.username1, cls.password1) cls.web_user1.eula.signed = True cls.web_user1.save() cls.username2 = 'ews_user2' cls.password2 = 'dummy' cls.web_user2 = WebUser.create(TEST_DOMAIN, cls.username2, cls.password2) cls.web_user2.get_domain_membership( TEST_DOMAIN).location_id = cls.test_facility3.location_id cls.web_user2.eula.signed = True cls.web_user2.save() cls.username3 = 'ews_user3' cls.password3 = 'dummy' cls.web_user3 = WebUser.create(TEST_DOMAIN, cls.username3, cls.password3) cls.web_user3.get_domain_membership( TEST_DOMAIN).location_id = cls.testregion2.location_id cls.web_user3.eula.signed = True cls.web_user3.save() cls.username4 = 'ews_user4' cls.password4 = 'dummy' cls.web_user4 = WebUser.create(TEST_DOMAIN, cls.username4, cls.password4) cls.web_user4.get_domain_membership( TEST_DOMAIN).location_id = cls.rsp.location_id cls.web_user4.eula.signed = True cls.web_user4.save() cls.username5 = 'ews_user5' cls.password5 = 'dummy' cls.web_user5 = WebUser.create(TEST_DOMAIN, cls.username5, cls.password5) domain_membership = cls.web_user5.get_domain_membership(TEST_DOMAIN) domain_membership.location_id = cls.test_district.location_id domain_membership.role_id = UserRole.get_read_only_role_by_domain( cls.domain.name).get_id cls.web_user5.eula.signed = True cls.web_user5.save() cls.ad = SQLProduct.objects.get(domain=TEST_DOMAIN, code='ad') cls.al = SQLProduct.objects.get(domain=TEST_DOMAIN, code='al') cls.client = Client()
class SMSUsersSyncTest(TestCase): def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.api_object = EWSApi(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) self.api_object.prepare_commtrack_config() self.api_object.create_or_edit_roles() for user in CommCareUser.by_domain(TEST_DOMAIN): user.delete() for verified_number in VerifiedNumber.by_domain(TEST_DOMAIN): verified_number.delete() def test_create_smsuser(self): with open(os.path.join(self.datapath, 'sample_smsusers.json')) as f: smsuser = SMSUser(json.loads(f.read())[0]) self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN))) ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) self.assertIsNotNone(ewsghana_smsuser.get_id) username_part = "%s%d" % (ewsghana_smsuser.name.strip().replace(' ', '.').lower(), smsuser.id) username = "******" % (username_part, TEST_DOMAIN) self.assertEqual(username, ewsghana_smsuser.username) self.assertEqual(smsuser.is_active, str(ewsghana_smsuser.is_active)) self.assertEqual(False, ewsghana_smsuser.is_superuser) self.assertEqual(False, ewsghana_smsuser.is_staff) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) self.assertEqual(verified_number.phone_number, '2222222222') domain_name = ewsghana_smsuser.get_domains()[0] self.assertEqual(TEST_DOMAIN, domain_name) self.assertIsInstance(ewsghana_smsuser.user_data['role'], list) self.assertEqual(len(ewsghana_smsuser.user_data['role']), 1) self.assertEqual(ewsghana_smsuser.user_data['role'][0], 'Other') def test_edit_smsuser(self): with open(os.path.join(self.datapath, 'sample_smsusers.json')) as f: smsuser = SMSUser(json.loads(f.read())[0]) self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN))) ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) user_id = ewsghana_smsuser.get_id self.assertIsNotNone(user_id) smsuser.phone_numbers = ['111111111'] ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) self.assertEqual(user_id, ewsghana_smsuser.get_id) self.assertEqual(ewsghana_smsuser.default_phone_number, '111111111') self.assertListEqual(ewsghana_smsuser.phone_numbers, ['111111111']) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) self.assertEqual(verified_number.phone_number, '111111111') def test_edit_phone_number1(self): """ When phone number is deleted on EWS side it also should be deleted in HQ :return: """ with open(os.path.join(self.datapath, 'sample_smsusers.json')) as f: smsuser = SMSUser(json.loads(f.read())[0]) self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN))) ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) smsuser.phone_numbers = [] ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) self.assertIsNone(ewsghana_smsuser.default_phone_number) self.assertListEqual(ewsghana_smsuser.phone_numbers, []) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNone(verified_number) def test_edit_phone_number2(self): """ When phone number is added on EWS side it also should be added in HQ """ with open(os.path.join(self.datapath, 'sample_smsusers.json')) as f: smsuser = SMSUser(json.loads(f.read())[0]) smsuser.phone_numbers = [] self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN))) ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNone(verified_number) smsuser.phone_numbers = ['111111111'] ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) self.assertIsNotNone(ewsghana_smsuser.default_phone_number) self.assertListEqual(ewsghana_smsuser.phone_numbers, ['111111111']) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) self.assertEqual(verified_number.phone_number, '111111111') def test_edit_phone_number3(self): """ When phone number is edited on EWS side it also should be edited in HQ """ with open(os.path.join(self.datapath, 'sample_smsusers.json')) as f: smsuser = SMSUser(json.loads(f.read())[0]) self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN))) ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) self.assertEqual(verified_number.phone_number, '2222222222') smsuser.phone_numbers = ['111111111'] ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) self.assertIsNotNone(ewsghana_smsuser.default_phone_number) self.assertListEqual(ewsghana_smsuser.phone_numbers, ['111111111']) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) self.assertEqual(verified_number.phone_number, '111111111') def test_edit_phone_number4(self): """ Number shouldn't be changed when is not edited on EWS side. """ with open(os.path.join(self.datapath, 'sample_smsusers.json')) as f: smsuser = SMSUser(json.loads(f.read())[0]) self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN))) ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) self.assertEqual(verified_number.phone_number, '2222222222') ewsghana_smsuser = self.api_object.sms_user_sync(smsuser) verified_number = ewsghana_smsuser.get_verified_number() self.assertIsNotNone(verified_number) self.assertEqual(verified_number.phone_number, '2222222222')
def ews_add_products_to_locs(request, domain): config = EWSGhanaConfig.for_domain(domain) endpoint = GhanaEndpoint.from_config(config) add_products_to_loc.delay(EWSApi(domain=domain, endpoint=endpoint)) return HttpResponse('OK')
def ews_resync_web_users(request, domain): config = EWSGhanaConfig.for_domain(domain) endpoint = GhanaEndpoint.from_config(config) resync_web_users.delay(EWSApi(domain=domain, endpoint=endpoint)) return HttpResponse('OK')
class TestStockDataSync(TestCase): def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.stock_api_object = MockEWSStockDataSynchronization( TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) self.api_object = EWSApi(TEST_DOMAIN, self.endpoint) self.api_object.prepare_commtrack_config() config = EWSGhanaConfig() config.domain = TEST_DOMAIN config.enabled = True config.all_stock_data = True config.password = '******' config.username = '******' config.url = 'http://test-api.com/' config.save() l1 = Location(name='Test location 1', external_id='3445', location_type='Hospital', domain=TEST_DOMAIN) l2 = Location(name='Test location 2', external_id='4407', location_type='Hospital', domain=TEST_DOMAIN) l1.save() l2.save() SupplyPointCase.create_from_location(TEST_DOMAIN, l1) SupplyPointCase.create_from_location(TEST_DOMAIN, l2) l1.save() l2.save() with open(os.path.join(self.datapath, 'sample_products.json')) as f: for product_json in json.loads(f.read()): self.api_object.product_sync(Product(product_json)) def test_stock_data_migration(self): stock_data_task(self.stock_api_object) self.assertEqual( StockTransaction.objects.filter( report__domain=TEST_DOMAIN).count(), 11) self.assertEqual(StockState.objects.all().count(), 8) self.assertEqual( StockTransaction.objects.filter(subtype='inferred').count(), 1) self.assertEqual(StockReport.objects.all().count(), 1) def test_stock_data_migration2(self): StockDataCheckpoint.objects.create(domain=TEST_DOMAIN, api='', limit=1000, offset=0, date=datetime.datetime(2015, 7, 1)) stock_data_task(self.stock_api_object) self.assertEqual( StockTransaction.objects.filter( report__domain=TEST_DOMAIN).count(), 11) self.assertEqual(StockState.objects.all().count(), 8) self.assertEqual( StockTransaction.objects.filter(subtype='inferred').count(), 0) self.assertEqual(StockReport.objects.all().count(), 1) def tearDown(self): Domain.get_by_name(TEST_DOMAIN).delete()
class TestStockDataSync(TestCase): def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.stock_api_object = MockEWSStockDataSynchronization(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) self.api_object = EWSApi(TEST_DOMAIN, self.endpoint) self.api_object.prepare_commtrack_config() config = EWSGhanaConfig() config.domain = TEST_DOMAIN config.enabled = True config.all_stock_data = True config.password = '******' config.username = '******' config.url = 'http://test-api.com/' config.save() l1 = Location( name='Test location 1', external_id='3445', location_type='Hospital', domain=TEST_DOMAIN ) l2 = Location( name='Test location 2', external_id='4407', location_type='Hospital', domain=TEST_DOMAIN ) l1.save() l2.save() SupplyPointCase.create_from_location(TEST_DOMAIN, l1) SupplyPointCase.create_from_location(TEST_DOMAIN, l2) l1.save() l2.save() with open(os.path.join(self.datapath, 'sample_products.json')) as f: for product_json in json.loads(f.read()): self.api_object.product_sync(Product(product_json)) def test_stock_data_migration(self): stock_data_task(self.stock_api_object) self.assertEqual(StockTransaction.objects.filter(report__domain=TEST_DOMAIN).count(), 11) self.assertEqual(StockState.objects.all().count(), 8) self.assertEqual(StockTransaction.objects.filter(subtype='inferred').count(), 1) self.assertEqual(StockReport.objects.all().count(), 1) def test_stock_data_migration2(self): StockDataCheckpoint.objects.create( domain=TEST_DOMAIN, api='', limit=1000, offset=0, date=datetime.datetime(2015, 7, 1) ) stock_data_task(self.stock_api_object) self.assertEqual(StockTransaction.objects.filter(report__domain=TEST_DOMAIN).count(), 11) self.assertEqual(StockState.objects.all().count(), 8) self.assertEqual(StockTransaction.objects.filter(subtype='inferred').count(), 0) self.assertEqual(StockReport.objects.all().count(), 1) def tearDown(self): Domain.get_by_name(TEST_DOMAIN).delete()
class LocationSyncTest(TestCase): def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.api_object = EWSApi(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) self.api_object.prepare_commtrack_config() for location in CouchLocation.by_domain(TEST_DOMAIN): location.delete() for sql_location in SQLLocation.objects.all(): sql_location.delete() def test_create_non_facility_location(self): with open(os.path.join(self.datapath, 'sample_locations.json')) as f: location = Location(json.loads(f.read())[0]) ewsghana_location = self.api_object.location_sync(location) self.assertEqual(ewsghana_location.name, location.name) self.assertEqual(ewsghana_location.location_type, location.type) self.assertEqual(ewsghana_location.longitude, float(location.longitude)) self.assertEqual(ewsghana_location.latitude, float(location.latitude)) self.assertEqual(ewsghana_location.parent, location.parent_id) self.assertFalse(ewsghana_location.is_archived) sql_location = ewsghana_location.sql_location self.assertEqual(ewsghana_location.get_id, sql_location.location_id) self.assertIsNotNone(sql_location.id) self.assertIsNone(ewsghana_location.linked_supply_point()) self.assertIsNone(sql_location.supply_point_id) def test_create_facility_location(self): with open(os.path.join(self.datapath, 'sample_locations.json')) as f: location = Location(json.loads(f.read())[1]) with open(os.path.join(self.datapath, 'sample_products.json')) as f: for p in json.loads(f.read()): self.api_object.product_sync(Product(p)) self.assertEqual(8, SQLProduct.objects.filter(domain=TEST_DOMAIN).count()) ewsghana_location = self.api_object.location_sync(location) self.assertEqual(ewsghana_location.name, location.supply_points[0].name) self.assertEqual(ewsghana_location.site_code, location.supply_points[0].code) self.assertEqual("Hospital", ewsghana_location.location_type) self.assertEqual(ewsghana_location.longitude, float(location.longitude)) self.assertEqual(ewsghana_location.latitude, float(location.latitude)) self.assertFalse(ewsghana_location.is_archived) sql_location = ewsghana_location.sql_location self.assertEqual(ewsghana_location.get_id, sql_location.location_id) self.assertEqual(int(sql_location.parent.external_id), location.parent_id) self.assertIsNotNone(sql_location.id) self.assertIsNotNone(sql_location.supply_point_id) supply_point = SupplyPointCase.get_by_location_id(TEST_DOMAIN, sql_location.location_id) self.assertIsNotNone(supply_point) self.assertEqual(location.supply_points[0].id, int(supply_point.external_id)) self.assertEqual(location.supply_points[0].name, supply_point.name) self.assertListEqual(location.supply_points[0].products, [product.code for product in ewsghana_location.sql_location.products]) def test_create_region_with_two_supply_points(self): with open(os.path.join(self.datapath, 'sample_locations.json')) as f: location = Location(json.loads(f.read())[4]) ewsghana_location = self.api_object.location_sync(location) self.assertEqual(2, SQLLocation.objects.filter( domain=TEST_DOMAIN, location_type__administrative=False).count() ) self.assertIsNone(ewsghana_location.linked_supply_point()) self.assertIsNone(ewsghana_location.sql_location.supply_point_id) self.assertEqual(location.name, ewsghana_location.sql_location.name) self.assertEqual(location.code, ewsghana_location.sql_location.site_code) self.assertFalse(ewsghana_location.is_archived) def test_facility_without_supply_point(self): with open(os.path.join(self.datapath, 'sample_locations.json')) as f: location = Location(json.loads(f.read())[5]) ewsghana_location = self.api_object.location_sync(location) self.assertEqual(1, SQLLocation.objects.filter( domain=TEST_DOMAIN, location_type__administrative=False).count() ) self.assertIsNotNone(ewsghana_location.linked_supply_point()) self.assertIsNotNone(ewsghana_location.sql_location.supply_point_id) self.assertEqual(ewsghana_location.name, location.name) self.assertEqual(ewsghana_location.site_code, location.code) self.assertTrue(ewsghana_location.is_archived) def test_facility_with_inactive_and_active_supply_point(self): with open(os.path.join(self.datapath, 'sample_locations.json')) as f: location = Location(json.loads(f.read())[6]) ewsghana_location = self.api_object.location_sync(location) self.assertEqual("tsactive", ewsghana_location.site_code) self.assertEqual("Active Test hospital", ewsghana_location.name) self.assertFalse(ewsghana_location.is_archived)