def get_andela_api_cohort_location_data(): authorization_token = os.environ.get('DEV_TOKEN') url = os.environ.get('ANDELA_API_URL') public_key_token = os.environ.get('PUBLIC_KEY') cohorts = [] centers = [] if public_key_token and authorization_token and url: try: public_key = base64.b64decode(public_key_token).decode("utf-8") # decode token payload = verify_token(authorization_token, public_key, "andela.com", "accounts.andela.com") print('\n\n Getting Data from API : ', payload.get('UserInfo').get('first_name')) Bearer = 'Bearer ' headers = {'Authorization': Bearer + authorization_token} cohort_data_response = requests.get(url + 'cohorts', headers=headers).json() location_data_response = requests.get(url + 'locations', headers=headers).json() # test centers locations = {} for location in location_data_response.get('values'): name = location.get("name") locations[name] = Center(name=name.lower(), uuid=location.get('id')) centers = list(locations.values()) # cohorts cohorts = [] for cohort_information in cohort_data_response.get('values'): name = cohort_information.get('name') center = locations.get( cohort_information.get('location').get('name')) cohort = Cohort(name=name.lower(), uuid=cohort_information.get('id'), center_id=center.uuid) cohorts.append(cohort) return tuple(cohorts), tuple(centers) except ExpiredSignatureError: print("The authorization token supplied is expired.") except JWTError: print("Something went wrong while validating your token.") except Exception: print("Your initial dev-data, won't work...: I DON'T KNOW WHY.") finally: return tuple(cohorts), tuple(centers)
def test_dev_user_seed_data(args): (nairobi, phoenix, roles) = args # cohorts cohort_14_ke = Cohort(name='Cohort 14 Test', center=nairobi, society=phoenix) # users # member member = User( uuid="-KdQsMtixI2U0y_-yJEH", name="Test User", photo="https://lh6.googleusercontent.com/-1DhBLOJentg/AAAAAAAAA" "AI/AAAAAAAAABc/ImeP_cAI/photo.jpg?sz=50", email="*****@*****.**", center=nairobi, cohort=cohort_14_ke, society=phoenix) member.roles.extend([roles[2], roles[4]]) # president president = User( uuid="-KdQsMtixG4U0y_-yJEH", name="Test President", photo="https://lh6.googleusercontent.com/-1DhBLOJentg/AAAAAAAAA" "AI/AAAAAAnAABc/ImeP_cAI/photo.jpg?sz=50", email="*****@*****.**", center=nairobi, cohort=cohort_14_ke, society=phoenix) president.roles.extend([roles[2], roles[4], roles[5]]) # success ops success_ops = User( uuid="-KdQsMtixG4U0y_-yJEF", name="Test success ops", photo="https://lh6.googleusercontent.com/-1DhBLOJentg/AAAAAAAAA" "AI/AAAAAAnAABc/ImeP_cAI/photo.jpg?sz=50", email="*****@*****.**", center=nairobi) success_ops.roles.extend([roles[3], roles[4]]) return (member, president, success_ops)
def add_extra_user_info( token, user_id, url=os.environ.get('ANDELA_API_URL')): # pragma: no cover """Retrive user information from ANDELA API. params: token(str): valid jwt token user_id(str): id for user to retive information about Returns: tuple(location, cohort, api_response) """ cohort = location = None Bearer = 'Bearer ' headers = {'Authorization': Bearer + token} try: api_response = requests.get(url + f"users/{user_id}", headers=headers) except requests.exceptions.ConnectionError: response = Response() response.status_code = 503 response.json = lambda: {"Error": "Network Error."} return cohort, location, response except Exception: response = Response() response.status_code = 500 response.json = lambda: {"Error": "Something went wrong."} return cohort, location, response if api_response.status_code == 200 and api_response.json().get('cohort'): cohort = Cohort.query.filter_by( uuid=api_response.json().get('cohort').get('id')).first() if not cohort: cohort = Cohort(uuid=api_response.json().get('cohort').get('id'), name=api_response.json().get('cohort').get('name')) location = Center.query.filter_by( uuid=api_response.json().get('location').get('id')).first() if not location: location = Center( uuid=api_response.json().get('location').get('id')) return cohort, location, api_response
def setUp(self): """Configure test enviroment.""" os.environ['APP_SETTINGS'] = 'Testing' self.patcher = mock.patch( 'api.services.auth.helpers.add_extra_user_info', return_value=(None, None, None)) self.patcher.start() self.app = create_app("Testing") self.app_context = self.app.app_context() self.app_context.push() db.drop_all() db.create_all() # test client self.client = self.app.test_client() token_payloads_list = [ self.incomplete_payload, self.expired_payload, self.test_cio_role_payload, self.test_society_president_role_payload, self.test_auth_role_payload, self.test_finance_payload, self.test_successops_payload ] for token_payload in token_payloads_list: token_payload.update({ 'iss': self.app.config['API_ISSUER'], 'aud': self.app.config['API_AUDIENCE'] }) self.header = { "Authorization": self.generate_token(self.test_user_payload), "Content-Type": "application/json" } self.success_ops = { "Authorization": self.generate_token(self.test_successops_payload), "Content-Type": "application/json" } self.society_president = { "Authorization": self.generate_token(self.test_society_president_role_payload), "Content-Type": "application/json" } self.sparks_society_president = { "Authorization": self.generate_token(self.test_sparks_society_president_payload), "Content-Type": "application/json" } self.society_secretary = { "Authorization": self.generate_token(self.test_society_secretary_payload), "Content-Type": "application/json" } self.cio = { "Authorization": self.generate_token(self.test_cio_role_payload), "Content-Type": "application/json" } self.finance = { "Authorization": self.generate_token(self.test_finance_payload), "Content-Type": "application/json" } self.bad_token_header = { "Authorization": self.generate_token({"I don't know": "what to put here"}), "Content-Type": "application/json" } # test centers self.nairobi = Center(name='Nairobi') self.kampala = Center(name='Kampala') self.lagos = Center(name='Lagos') # test societies self.phoenix = Society(name="Phoenix", color_scheme="#00001", logo="https://bit.ly/2FTjkbV", photo="https://bit.ly/2k2l0qx") self.istelle = Society(name="iStelle", color_scheme="#00002", logo="https://bit.ly/2FTjkbV", photo="https://bit.ly/2k2l0qx") self.sparks = Society(name="Sparks", color_scheme="#00003", logo="https://bit.ly/2FTjkbV", photo="https://bit.ly/2k2l0qx") self.invictus = Society(name="Invictus", color_scheme="#00004", logo="https://bit.ly/2FTjkbV", photo="https://bit.ly/2k2l0qx") # test roles self.successops_role = Role(uuid="-KkLwgbeJUO0dQKsEk1i", name="success ops") self.fellow_role = Role(uuid="-KXGy1EB1oimjQgFim6C", name="Fellow") self.success_role = Role(uuid="-KXGy1EB1oimjQgFim6F", name="success ops") self.finance_role = Role(uuid="-KXGy1EB1oimjQgFim6L", name="finance") self.lf_role = Role(uuid="d47ec8a7-3f09-44a5-8188-ff1d40ef35b6", name="Learning Facilitator") self.president_role = Role(uuid="-KXGyd2udi2", name="society president") self.v_president_role = Role(uuid="-KXGy32odnd", name="vice president") self.secretary_role = Role(uuid="-KXGy12odfn2idn", name="society secretary") self.cio_role = Role(uuid="-KXGionceu24i2y", name="cio") # test cohorts self.cohort_12_Ke = Cohort(name="cohort-12", center=self.nairobi) self.cohort_14_Ke = Cohort(name="cohort-14", center=self.nairobi) self.cohort_12_Ug = Cohort(name="cohort-12", center=self.kampala) self.cohort_1_Nig = Cohort(name="cohort-1", center=self.lagos) # test users self.test_user = User(uuid="-KdQsMt2U0ixIy_-yWTSZ", name="Test User", photo="https://www.link.com", email="*****@*****.**", center=self.lagos, cohort=self.cohort_1_Nig, society=self.phoenix) self.test_user_2 = User(uuid="-KdQsawesome_usedk2cckjfbi", name="Test User2", photo="https://www.link.com", email="*****@*****.**", center=self.kampala, cohort=self.cohort_12_Ug, society=self.sparks) self.test_user_3 = User(uuid="-KdQsawesomb2dunkdnw", name="Test User3", photo="https://www.link.com", email="*****@*****.**", center=self.nairobi, cohort=self.cohort_12_Ke, society=self.invictus) self.test_cio = User( uuid="-KdQdsdadqwdqomb2dunkdnw", name="Test CIO", photo="https://www.link.com", email="*****@*****.**", center=self.nairobi, ) self.test_cio.roles.append(self.cio_role) self.president = User( uuid="-KdQsMtixG4U0y_-yJEH", name="Test President", photo="https://lh6.googleusercontent.com/-1DhBLOJentg/AAAAAAAAA" "AI/AAAAAAnAABc/ImeP_cAI/photo.jpg?sz=50", email="*****@*****.**", center=self.lagos, cohort=self.cohort_1_Nig, society=self.phoenix) self.president.roles.append(self.president_role) self.vice_president = User( uuid="-KdQsMtixGc2nuekwnd", name="Test Vice-President", photo="https://lh6.googleusercontent.com/-1DhBLOJentg/AAAAAAAAA" "AI/AAAAAAnAABc/ImeP_cAI/photo.jpg?sz=50", email="*****@*****.**", center=self.nairobi, cohort=self.cohort_12_Ke, society=self.sparks) self.vice_president.roles.append(self.v_president_role) self.secretary = User( uuid="-Kuty7hryt8cbkc", name="Test Secretary", photo="https://lh6.googleusercontent.com/-1DhBLOJentg/AAAAAAAAA" "AI/AAAAAAnAABc/ImeP_cAI/photo.jpg?sz=50", email="*****@*****.**", center=self.nairobi, cohort=self.cohort_14_Ke, society=self.invictus) self.secretary.roles.append(self.secretary_role) # test ActivityType self.hackathon = ActivityType(name="Hackathon", description="A Hackathon", value=100) self.tech_event = ActivityType(name="Tech Event", description="Organize a tech event", value=2500) self.interview = ActivityType( name="Bootcamp Interviews", description="Interviewing candidate for a fellow" " recruiting event", value=20, supports_multiple_participants=True) # test Activity self.alibaba_ai_challenge = Activity( name='Fashion challenge', activity_type=self.hackathon, activity_date=datetime.date.today() + datetime.timedelta(days=21), added_by=self.president) self.js_meet_up = Activity(name='Nairobi Js meetup', activity_type=self.tech_event, activity_date=datetime.date.today() + datetime.timedelta(days=14), added_by=self.president) self.bootcamp_xiv = Activity(name='Bootcamp XIV Interviews - Nairobi', activity_type=self.interview, activity_date=datetime.date.today() + datetime.timedelta(days=14), added_by=self.president) # test LoggedActivity self.log_alibaba_challenge = LoggedActivity( name="my logged activity", description="Participated in this event", value=2500, user=self.test_user, activity=self.alibaba_ai_challenge, society=self.invictus, activity_type=self.hackathon) self.log_alibaba_challenge2 = LoggedActivity( name="my second logged activity", description="Participated in this event", value=2500, user=self.test_user, activity=self.alibaba_ai_challenge, society=self.sparks, activity_type=self.hackathon) self.redemp_req = RedemptionRequest(name="T-shirt Funds Request", value=2500, user=self.test_user, center=self.test_user.center, society=self.test_user.society) # save common items to db self.tech_event.save() self.interview.save() self.hackathon.save() self.test_user.save()