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

Write UT for cryptoUtils module #287

Closed
muke1908 opened this issue May 8, 2023 · 12 comments
Closed

Write UT for cryptoUtils module #287

muke1908 opened this issue May 8, 2023 · 12 comments

Comments

@muke1908
Copy link
Owner

muke1908 commented May 8, 2023

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
Copy link
Contributor

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
Copy link
Owner Author

muke1908 commented May 24, 2023

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

@estefysc
Copy link
Contributor

@muke1908 perfect! I will. Thanks.

@estefysc
Copy link
Contributor

estefysc commented Jun 7, 2023

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
Copy link
Owner Author

muke1908 commented Jun 7, 2023

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

@estefysc
Copy link
Contributor

estefysc commented Jun 8, 2023

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

@estefysc
Copy link
Contributor

estefysc commented Jun 9, 2023

@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
Copy link
Owner Author

muke1908 commented Jun 9, 2023

@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
Copy link
Contributor

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

@estefysc
Copy link
Contributor

@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
Copy link
Owner Author

muke1908 commented Jun 17, 2023 via email

@estefysc
Copy link
Contributor

Ok, just did.

muke1908 added a commit that referenced this issue Jun 30, 2023
* initiated work - started template

* added some tests

* changed the mock implementation

* testing code

* fix: added jest config, added mock

* working on mocks

* working on mocks and encryptMessage tests

* Tests finished

* Update service/src/crypto.test.ts

---------

Co-authored-by: Estefania Sanchez <estefaniasanchez@Estefanias-Work-MacBook-Air.local>
Co-authored-by: Mukesh <mukeshbiswas@microsoft.com>
Co-authored-by: Estefania SanchezCabrejos <esanchezcabrejos@ncf.edu>
Co-authored-by: Mukesh <mukeshbsws@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants