mardi 30 juin 2015

Python unittests cannot find email templates?

So I am working on some unit tests but I am running into a problem where when I testing to see if a new file is created or to see if an email is being sent the unit test either can't find the place to find the file or fails to create the file in the specified location. I know it works because when I run the code everything works flawlessly but when I try to run the code using the unittest module it fails the test. Any ideas why?

def generate_token(self, username):

    unique_id = str(uuid.uuid4()).replace("-", "").upper()

    file_path = os.path.abspath(os.path.join(os.getcwd(), 'tokens'))
    file_path = os.path.join(file_path, unique_id + '.txt')

    print file_path

    try:
        data = {
            "username": username,
            "date_created": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        }
        file = open(file_path, "a")
        json.dump(data, file, indent=4)
        file.close()
        if(app.config['DEBUG']): print("File Created!")
    except:
        if(app.config['DEBUG']): print("Could not create new file!")

    return unique_id

In this method it cannot create the new token file for some reason

def __send_password_reset_email(self, first_name, username, email):
    is_prod = app.config['IS_PROD']
    root_url = app.config['DEV_URL']
    mail_host = app.config['DEV_MAIL_SERVER']
    if(is_prod):
        mail_host = app.config['PROD_MAIL_SERVER']
        root_url = app.config['PROD_URL']

    file = open('./emails/email-pwreset.htm', 'r')
    msg = file.read()
    file.close()

    msg = msg.replace('[FIRSTNAME]', first_name)
    msg = msg.replace('[USERNAME]', username)
    msg = msg.replace('[WEBSITEROOTURL]', root_url)

    mail_server = smtplib.SMTP(mail_host)
    msg = MIMEText(msg, 'html')
    from_addr = app.config['TAYLOR_EMAIL']

    msg['Subject'] = 'Your Taylor Password Has Been Reset'
    msg['From'] = from_addr
    msg['To'] = email

    if(not app.config['TESTING']):
        try:
            mail_server.sendmail(from_addr, email, msg.as_string())
        except smtplib.SMTPException, e:
            print(e)

    mail_server.quit()

It fails here when it tries to find the email folder.

I honestly have no idea what is wrong and I have no idea where to start to try and fix this.

Aucun commentaire:

Enregistrer un commentaire