muke1908 / chat-e2ee

End-to-end encrypted disposable chat sessions, exchange sensitive information with peer safely and securely.
https://chat-e2ee-2.azurewebsites.net/
Apache License 2.0
323 stars 198 forks source link

Write UT for cryptoUtils module #287

Closed muke1908 closed 1 year ago

muke1908 commented 1 year ago

Write unit test for cryptoUtils module, mock all necessary external browser dependency. https://github.com/muke1908/chat-e2ee/blob/master/service/src/crypto.ts

estefysc commented 1 year ago

Hey, @muke1908. Would like to work on these tests as well! Might take me a bit longer though. Let me know if thats ok with you.

muke1908 commented 1 year ago

@estefysc sure, go ahead! Feel free to write if you need any help

estefysc commented 1 year ago

@muke1908 perfect! I will. Thanks.

estefysc commented 1 year ago

Hey @muke1908 just wanted to double check.. are the tests just for "cryptoUtils", meaning the functions generateKeypairs, encryptMessage, and decryptMessage? Or should I include tests for all the other functions in the file?

muke1908 commented 1 year ago

@estefysc Yes those three are enough as only they are exported.

estefysc commented 1 year ago

@muke1908 ok, perfect. Thanks for your response. Will finish soon for you to review and make suggestions.

estefysc commented 1 year ago

@muke1908 I am having issues mocking the window.crypto.subtle dependency. I have done some research, and I have found info that talks about using the node crypto.webcrypto property like so:

const crypto = require('crypto').webcrypto;
global.crypto = crypto;

Would this be ok?

muke1908 commented 1 year ago

@estefysc You should not use the external module for testing. It needs to be mocked. You can just assign some mock module to global.crypto like

global.crypto = { 
subtle: {
        generateKey: jest.fn(),
        encrypt: jest.fn(),
        decrypt: jest.fn(),
        importKey: jest.fn(),
        exportKey: jest.fn()
    }
}

also, you need to add mockReturnvalue to each func. For example, for generateKey:

const subtle = {
      generateKey: jest.fn().mockResolvedValue('generatedKeyPair'),
};

globalThis.crypto = {
      subtle,
};

it should work

estefysc commented 1 year ago

@muke1908 Perfect. Thanks for the explanation. Will work in it.

estefysc commented 1 year ago

@muke1908 I tried your suggestion and a couple versions of it, but I am seeing the following error in all cases:

The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string.
    Consider using the "jsdom" test environment.

    ReferenceError: window is not defined
muke1908 commented 1 year ago

can You push your code and create a PR, I’ll check On Sat 17. Jun 2023 at 23:28, estefysc @.***> wrote:

@muke1908 https://github.com/muke1908 I tried your suggestion and a couple versions of it, but I am seeing the following error in all cases:

The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string. Consider using the "jsdom" test environment.

ReferenceError: window is not defined

— Reply to this email directly, view it on GitHub https://github.com/muke1908/chat-e2ee/issues/287#issuecomment-1595864593, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE23SBLI2HHDUBQYAMN4OG3XLYOPZANCNFSM6AAAAAAX2D7QEY . You are receiving this because you were mentioned.Message ID: @.***>

estefysc commented 1 year ago

Ok, just did.