def test_user_remove_without_voucher(self): user_id = 'test_user_get_id' certification_level = 'test_user_get_level' #given self.user_table.put_item(Item={'user_id': user_id, 'certification_level': certification_level}) user = User(user_id, certification_level) #when response = user.remove() #then assert response == True users = self.user_table.scan(FilterExpression=Attr('user_id').eq(user_id))['Items'] assert len(users) == 0
def getVoucher(self): payload = None user = User.get(self.user_id) if user: for user_certification in user.user_certifications: if not user_certification.voucher_code: try: voucher = Voucher.getAvailable( int(user_certification.level_id)) user_certification.attribuateVoucher(voucher) except Exception as e: self.logger.warn(e) payload = { "blocks": [{ "type": "section", "block_id": "section001", "text": { "type": "mrkdwn", "text": "Hi! Here are your personal voucher codes" } }, { "type": "section", "block_id": "section002", "fields": user.formatSlackFields() }] } else: payload = { "text": "Hi! Unfortunately we couldn't find your personal voucher code in our data base. Please get back to <@UBRJ09SBE>." } return payload
def test_user_passesCertification_without_voucher(self): user_id = 'test_user_passes_id' level_name = 'test_user_passes_level_name' certification_level = 'test_user_passes_certification_level' level_stars = 1 #given user = User(user_id, certification_level) level = Level(certification_level, level_name, level_stars) #when response = user.passesCertification(level) #then assert response == False assert user.profile_update_date == None
def test_user_add(self): user_id = 'test_user_get_id' certification_level = 'test_user_get_level' #given user = User(user_id, certification_level) #when response = user.add() #then assert response == True users = self.user_table.scan(FilterExpression=Attr('user_id').eq(user_id))['Items'] assert len(users) == 1 assert users[0]['user_id'] == user_id assert users[0]['certification_level'] == certification_level
def test_user_passesCertification_already_saved(self): user_id = 'test_user_passes_id' level_name = 'test_user_passes_level_name' certification_level = 'test_user_passes_certification_level' level_stars = 1 voucher_code = 'test_user_passes_voucher_code' attribuated_date = 'test_user_passes_attribuated_date' profile_update_date = 'test_user_passes_profile_update_date' #given user = User(user_id, certification_level, voucher_code, attribuated_date, profile_update_date) level = Level(certification_level, level_name, level_stars) #when response = user.passesCertification(level) #then assert response == False assert user.profile_update_date == profile_update_date
def test_user_passesCertification(self): user_id = 'test_user_passes_id' level_id = 'test_level_passes_id' level_name = 'test_user_passes_level_name' level_stars = 1 voucher_code = 'test_user_passes_voucher_code' #given self.user_table.put_item(Item={'user_id': user_id, 'certification_level': level_id, 'voucher_code': voucher_code}) self.level_table.put_item(Item={'id': level_id, 'name': level_name}) user = User(user_id, level_id, voucher_code) level = Level(level_id, level_name, level_stars) #when response = user.passesCertification(level) #then assert response == True assert user.profile_update_date == time.strftime('%d/%m/%Y',time.localtime())
def test_user_attribuateVoucher_wrong_level(self): user_id = 'test_user_get_id' certification_level = 'test_user_get_level' voucher_code = 'test_user_get_voucher_code' voucher_level = 'test_user_get_voucher_level' voucher_availability = '01/01/2019' #given self.user_table.put_item(Item={'user_id': user_id, 'certification_level': certification_level}) self.voucher_table.put_item(Item={'code': voucher_code, 'certification_level': voucher_level}) user = User(user_id, certification_level) voucher = Voucher(voucher_code, voucher_level, voucher_availability) #when response = user.attribuateVoucher(voucher) #then assert response == False assert user.voucher_code == None
def test_user_attribuateVoucher(self): user_id = 'test_user_get_id' certification_level = 'test_user_get_level' voucher_code = 'test_user_get_voucher_code' voucher_availability = '01/01/2019' #given self.user_table.put_item(Item={'user_id': user_id, 'certification_level': certification_level}) self.voucher_table.put_item(Item={'code': voucher_code, 'certification_level': certification_level}) user = User(user_id, certification_level) voucher = Voucher(voucher_code, certification_level, voucher_availability) #when response = user.attribuateVoucher(voucher) #then assert response == True assert user.voucher_code == voucher_code assert user.attribuated_date == time.strftime('%d/%m/%Y',time.localtime())
def test_user_get_nonexistent(self): user_id = 'test_user_get_id' #given #when user = User.get(user_id) #then assert user == None
def test_user_str_without_voucher(self): user_id = 'test_user_str_id' certification_level = 'test_user_str_level' #given user = User(user_id, certification_level) #when response = str(user) #then assert response != None
def getUserVoucher(self): payload = None # Limited to admin users if not self.user_id in self.config.admin_users: payload = { "text": "Sorry this command is limited to the bot administrators." } # Check parameters elif not self.parameter: payload = {"text": "Usage: /getuservoucher @user"} else: # Get UDID from parameter (format: <@ABCD|username>) user_udid = '' user_name = '' try: user_udid = re.search('@(.+?)\|', self.parameter).group(1) user_name = re.search('\|(.+?)>', self.parameter).group(1) except AttributeError: user_udid = 'error' user_name = 'error' user = User.get(user_udid) if user: payload = { "blocks": [{ "type": "section", "block_id": "section001", "text": { "type": "mrkdwn", "text": "Hi! Here are personal voucher codes for <@" + user_udid + ">" } }, { "type": "section", "block_id": "section002", "fields": user.formatSlackFields() }] } else: payload = { "text": "Hi! Unfortunately we couldn't find the user <@" + user_udid + "> in our data base." } return payload
def test_user_get_without_voucher(self): user_id = 'test_user_get_id' certification_level = 'test_user_get_level' #given self.user_table.put_item(Item={'user_id': user_id, 'certification_level': certification_level}) #when user = User.get(user_id) #then assert user.user_id == user_id assert user.certification_level == certification_level assert user.voucher_code == None
def test_user_get_with_voucher(self): user_id = 'test_user_get_id' certification_level = 'test_user_get_level' voucher_code = 'test_user_get_voucher_code' attribuated_date = 'test_user_get_attribuated_date' #given self.user_table.put_item(Item={'user_id': user_id, 'certification_level': certification_level, 'voucher_code': voucher_code, 'attribuated_date': attribuated_date}) #when user = User.get(user_id) #then assert user.user_id == user_id assert user.certification_level == certification_level assert user.voucher_code == voucher_code assert user.attribuated_date == attribuated_date
def addUser(self, user_id, certification_level_id): response = "" user = User.get(user_id) levels = Level.getAll() certification_level = [ level.name for level in levels if level.id == certification_level_id ][0] if user: user_certification = [ user_certification.level_id for user_certification in user.user_certifications if user_certification.level_id == certification_level_id ] if user_certification: return { "text": "<@" + self.form['user_id'] + "> already get a subscription for certification level *" + certification_level + "*" } # New user or existing user with a new certification level, so we add him/her if UserCertification(user_id, certification_level_id).add(): response = { "text": "<@" + self.form['user_id'] + "> added with certification level *" + certification_level + "*" } else: response = { "text": "There is an error while adding <@" + self.form['user_id'] + ">" } return response
def launch(self, event): # Manage 'challenge' from Slack to validate the lambda. if "challenge" in event: return event["challenge"] slack_event = event['event'] # Ignore message from bot. if not "bot_id" in slack_event \ and slack_event['type'] == 'user_change' \ and 'XfELFP2WL9' in slack_event['user']['profile']['fields']: # Application configuration config = Configuration(self.logger, self.environment) # Check input token if not event['token'] in config.slack_event_token: return "403 Forbidden" self.logger.info(slack_event['user']['real_name'] + " gets " + slack_event['user']['profile']['fields'] ['XfELFP2WL9']['value'] + " certification!") user_udid = slack_event['user']['id'] user_level_name = re.search( ' \((.+?) level\)', slack_event['user']['profile']['fields'] ['XfELFP2WL9']['value'].lower()).group(1) user = User.get(user_udid) level = Level.getByName(user_level_name) if user and level: for user_certification in user.user_certifications: user_certification.passesCertification(level) return "200 OK"