def test_search_with_match_any(self): target_email = self.emails[1] unique_string = target_email.subject[0:10] criteria = SearchCriteria() criteria.subject = unique_string criteria.body = "this is a link" criteria.match = "ANY" results = self.client.messages.search(self.server, criteria).items self.assertEqual(4, len(results))
def test_search_by_body(self): target_email = self.emails[1] unique_string = target_email.subject[0:10] criteria = SearchCriteria() criteria.body = "%s html" % (unique_string) results = self.client.messages.search(self.server, criteria).items self.assertEqual(1, len(results)) self.assertEqual(target_email.to[0].email, results[0].to[0].email) self.assertEqual(target_email.subject, results[0].subject)
def test_notifications(self): global token token = self.getToken() global service_token service_token = self.getServiceToken() global base_tenant_id base_tenant_id = self.getBaseTenantId() global roleIdMap roleIdMap = self.getRoleIdMap() seconds_since_epoch = datetime.now().timestamp() user_ids = [] usernames = [] for i in range(NO_OF_SUBSCRIBERS): # Create User username = self.mailosaur.servers.generate_email_address( MAILOSAUR_SERVER_NAME) user_id = self.createUser(username) print("created user-id: " + user_id) user_ids.append(user_id) usernames.append(username) # Create tenant tenant_id = self.createTenant(tenant_name, usernames) print("created tenant-id: " + tenant_id) # Create subscription self.createSubscription(SERVICE_NAME, tenant_id, user_ids) body = "This is Sample Event one sent - " + str(seconds_since_epoch) execptionRaised = False try: # Generate and send notification ws = create_connection( ws_url + '/audit/ws/audit-ingest', header={"Authorization": "Bearer " + service_token}, sslopt={"cert_reqs": ssl.CERT_NONE}) # Start thread to listen to audit logs auditLogWatcher = CommandWatch( cmd='kubectl -n rainier logs -f --tail 0 rainier-audit-0', countdown=NO_OF_NOTIFICATIONS * NO_OF_SUBSCRIBERS, end_pattern=".*(Email Sent Successfully for:).*", start_pattern= ".*(Size of notification queue is:)\s*\d+\s+(Size of event queue is:)\s*\d+" ) audit_run_task = auditLogWatcher.submit(TIMEOUT) print("Sending " + str(NO_OF_NOTIFICATIONS) + " notification, Body: " + body) msg = stomper.Frame() msg.cmd = 'SEND' msg.headers = { 'destination': '/events/post', 'content_type': 'application/json', 'receipt': 'new-receipt' } msg.body = json.dumps({ "tenant-id": tenant_id, "severity": "CRITICAL", "event-message": body, "event-name": SERVICE_NAME + "Asset", "time": int(round(time.time() * 1000)), "event-type": SERVICE_NAME + "down", "elementId": "oldvalue1", "neId": "newvalue1", "ne-kind": "userid6" }) for i in range(NO_OF_NOTIFICATIONS): ws.send(msg.pack()) ws.close() except Exception as err: print( "-----Exception while trying to send notification via websocket-----" ) print(err) print(traceback.print_exc()) print( "-------------------------------------------------------------------" ) execptionRaised = True exit(1) finally: if not execptionRaised: # Wait till all notifications are read from queue print("Checking audit logs .....") runtime = audit_run_task.result() print("Notifications processing time taken: " + str(runtime) + " s") # Delete subscription self.deleteSubscription(tenant_id) # Delete tenant self.deleteTenant(tenant_id) # Delete user self.deleteUser(user_ids) if validate: # Verify email notifications print("Waiting for emails to be sent .....") time.sleep(10) print("Done waiting") for user in usernames: # 2. Build search criteria to find the email you have sent print("Checking for user: "******"INFO Alert" criteria.sent_from = "*****@*****.**" criteria.body = body try: # 3. Wait for the message (by default only looks for messages received in the last hour) messages = self.mailosaur.messages.search( MAILOSAUR_SERVER_NAME, criteria) message_list = {} if len(messages.items) > 0: for message in messages.items: if message.id not in message_list: message_list[message.id] = message t_end = time.time() + 60 * 2 # wait for 2 mins i = 1 while time.time() < t_end and len( messages.items) < NO_OF_NOTIFICATIONS: time.sleep(10 * i) messages = self.mailosaur.messages.search( MAILOSAUR_SERVER_NAME, criteria) if len(messages.items) > 0: for message in messages.items: if message.id not in message_list: message_list[message.id] = message except MailosaurException as err: print(err.error_type) print(err.message) print("Response code: " + err.http_status_code + ", body: " + err.http_response_body) print(traceback.print_exc()) # 4. Assert that the email subject is what we expect self.assertEqual(NO_OF_NOTIFICATIONS, len(messages.items)) for key in message_list: message = self.mailosaur.messages.get_by_id(key) print("To: " + ",".join(str(p.email) for p in message.to)) print("Bcc: " + ",".join(str(p.email) for p in message.bcc)) print("Text Content: " + str(message.text.body)) print("HTML Content: " + str(message.html.body)) for message in messages.items: self.mailosaur.messages.delete(message.id)