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_get_nonexistent(self): user_id = 'test_user_get_id' #given #when user = User.get(user_id) #then assert user == 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"