Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bcrypt.compare always return false when executed in test stage #923

Open
TannicArturo98 opened this issue Mar 18, 2022 · 0 comments
Open

Comments

@TannicArturo98
Copy link

TannicArturo98 commented Mar 18, 2022

I wrote a function to authenticate users and in development and production stage work fine.
Then, i wrote a test using Jest v27.5.1 for that function and when i run the test, it always will return me "Wrong password" in the first test, where it should return the authToken.

Auth function:
`

export const authentication = async (email: string, password: string) => {
    try {
        // Retrieving data from DB using "sequelize"
        const authData = await Customer.findOne({
            attributes: ['id', 'password'],
            where: { email: email },
            raw: true
        });

        if (!authData)
            throw new Error('401|ALPHA-0010|Wrong credentials.');
       
        // In authData['password'] there is the hash of password
        const passwordCompare = await bcrypt.compare(password, authData['password']);
    
        if (!passwordCompare)
            throw new Error('401|ALPHA-0010|Wrong password.');

        return true;
    }
    catch (error: any) {
        console.error(error);
        return false;
    }
};

`

I was expecting that the first test passes because i pass as arguments a user that exists in my MySQL DB.
I'm using Node 17.6.0 on Windows 11.

Test:
`

describe('Authentication', () => {
    const credentials = {
        ok: {
            email: 'seed@test.com',
            password: 'Test98@'
        },
        wrong: {
            email: 'wrong@test.com',
            password: 'WrongTest98@'
        }
    };

    test('It should return true for successful authentication.', async () => {
        const result= await authentication(credentials.ok.email, credentials.ok.password);
        expect(result).toBe(true);
    });

    test('It should return false for worng credentials.', async () => {
        const result= await authentication(credentials.wrong.email, credentials.wrong.password);
        expect(result).toBe(false);
    });
});

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant