Skip to content

Commit

Permalink
Add unit tests for project functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
david96182 committed Apr 20, 2024
1 parent 0e40862 commit 873bd15
Show file tree
Hide file tree
Showing 11 changed files with 1,115 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,36 @@ To install Ninjemail, you can follow these steps:

You can then proceed to use Ninjemail as described in the next instructions.

## Testing

To ensure the reliability and correctness of this project, tests have been implemented using [pytest](https://pytest.org/). The test suite covers various aspects of the codebase and helps maintain the desired functionality as the project evolves.

### Running Tests

1. Make sure you have the project's dependencies installed.

2. Navigate to the project's root directory.

3. Run the following command to execute the test suite:

```bash
pytest
```

### Test Coverage

Comprehensive test coverage is prioritized to minimize bugs and regressions. The current code coverage can be measured using pytest-cov. To generate a coverage report, users can execute the following command:

```bash
pytest --cov
```

The coverage report will be displayed in the terminal.

### Writing Tests

When adding new features or fixing bugs, it's important to include corresponding test cases to validate the changes. Tests should be placed in the `tests/` directory, following a naming convention such as `test_module.py` or `test_class.py`. You can organize the tests based on the project's structure.

## Usage

### Importing the Library
Expand Down
Empty file added ninjemail/tests/__init__.py
Empty file.
167 changes: 167 additions & 0 deletions ninjemail/tests/test_email_gmail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import pytest
from unittest.mock import Mock, MagicMock
from selenium.webdriver.common.keys import Keys
from ..email_providers.gmail import create_account
from ..utils.webdriver_utils import create_driver

@pytest.fixture(autouse=True)
def mock_gmail(monkeypatch):
# Mock GeckoDriverManager and ChromeDriverManager installations
monkeypatch.setattr('webdriver_manager.firefox.GeckoDriverManager.install', lambda *args, **kwargs: 'gecko_driver_path')
monkeypatch.setattr('webdriver_manager.chrome.ChromeDriverManager.install', lambda *args, **kwargs: 'chrome_driver_path')

# Mock WebDriver classes
mock_firefox = Mock()
mock_chrome = Mock()
monkeypatch.setattr('selenium.webdriver.Firefox', lambda *args, **kwargs: mock_firefox)
monkeypatch.setattr('selenium.webdriver.Chrome', lambda *args, **kwargs: mock_chrome)

# Mock WebDriver methods
mock_firefox.get = MagicMock()
mock_chrome.get = MagicMock()
mock_firefox.find_element = MagicMock(return_value=Mock())
mock_chrome.find_element = MagicMock(return_value=Mock())
mock_firefox.find_elements = MagicMock(return_value=[Mock()])
mock_chrome.find_elements = MagicMock(return_value=[Mock()])
mock_firefox.quit = MagicMock()
mock_chrome.quit = MagicMock()

# Mock WebDriverWait and expected_conditions
mock_wait = MagicMock()
monkeypatch.setattr('selenium.webdriver.support.ui.WebDriverWait', lambda *args, **kwargs: mock_wait)
mock_wait.until = MagicMock(return_value=Mock())
monkeypatch.setattr('selenium.webdriver.support.expected_conditions.presence_of_element_located', lambda *args, **kwargs: MagicMock())
monkeypatch.setattr('selenium.webdriver.support.expected_conditions.element_to_be_clickable', lambda *args, **kwargs: MagicMock())

# Correctly mock Select class
def mock_select_init(self, *args, **kwargs):
self.select_by_index = MagicMock()
self.select_by_visible_text = MagicMock()
self.select_by_value = MagicMock()

monkeypatch.setattr('selenium.webdriver.support.ui.Select.__init__', mock_select_init)

# Mock Keys
monkeypatch.setattr('selenium.webdriver.common.keys.Keys', Keys)

# Mock time
monkeypatch.setattr('time.sleep', MagicMock(()))

# Mock logging
monkeypatch.setattr('logging.info', MagicMock())
monkeypatch.setattr('logging.error', MagicMock())

# Mock sms services
def mock_get_phone_getsmscode(self, *args, **kwargs):
return '111111111'

def mock_get_phone_smspool(self, *args, **kwargs):
return 'ordeid', '111111111'

def mock_get_code(self, *arg, **kwargs):
return '000000'

def mock_get_phone_fail(self, *args, **kwargs):
raise Exception("Failed to get phone number")

def mock_get_code_fail(self, *args, **kwargs):
raise Exception("Failed to retreive code")

def test_create_account_firefox_and_getsmscode(monkeypatch):
monkeypatch.setattr('sms_services.getsmscode.GetsmsCode.get_phone', mock_get_phone_getsmscode)
monkeypatch.setattr('sms_services.getsmscode.GetsmsCode.get_code', mock_get_code)

driver = create_driver('firefox')
# Test data
sms_key = {
"name": "getsmscode",
"data": {
"user": "username",
"token": "your_api_key",
}
}
username = "testuser"
password = "testpassword"
first_name = "John"
last_name = "Doe"
month = "1"
day = "1"
year = "2000"

email, password = create_account(driver, sms_key, username, password, first_name, last_name, month, day, year)
assert email == f"{username}@gmail.com"
assert password == "testpassword"

def test_create_account_chrome_and_smspool(monkeypatch):
monkeypatch.setattr('sms_services.smspool.SMSPool.get_phone', mock_get_phone_smspool)
monkeypatch.setattr('sms_services.smspool.SMSPool.get_code', mock_get_code)

driver = create_driver('chrome')
# Test data
sms_key = {
"name": "smspool",
"data": {
"token": "your_api_key",
}
}
username = "testuser"
password = "testpassword"
first_name = "John"
last_name = "Doe"
month = "1"
day = "1"
year = "2000"

email, password = create_account(driver, sms_key, username, password, first_name, last_name, month, day, year)
assert email == f"{username}@gmail.com"
assert password == "testpassword"

def test_create_account_phone_fail(monkeypatch):
monkeypatch.setattr('sms_services.getsmscode.GetsmsCode.get_phone', mock_get_phone_fail)

driver = create_driver('firefox')
# Test data
sms_key = {
"name": "getsmscode",
"data": {
"user": "username",
"token": "your_api_key",
}
}
username = "testuser"
password = "testpassword"
first_name = "John"
last_name = "Doe"
month = "1"
day = "1"
year = "2000"

with pytest.raises(Exception, match="Failed to get phone number"):
email, password = create_account(driver, sms_key, username, password, first_name, last_name, month, day, year)
assert email == None
assert password == None

def test_create_account_code_fail(monkeypatch):
monkeypatch.setattr('sms_services.smspool.SMSPool.get_phone', mock_get_phone_smspool)
monkeypatch.setattr('sms_services.smspool.SMSPool.get_code', mock_get_code_fail)

driver = create_driver('firefox')
# Test data
sms_key = {
"name": "smspool",
"data": {
"token": "your_api_key",
}
}
username = "testuser"
password = "testpassword"
first_name = "John"
last_name = "Doe"
month = "1"
day = "1"
year = "2000"

with pytest.raises(Exception, match="Failed to retreive code"):
email, password = create_account(driver, sms_key, username, password, first_name, last_name, month, day, year)
assert email == None
assert password == None
129 changes: 129 additions & 0 deletions ninjemail/tests/test_email_outlook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import pytest
from unittest.mock import Mock, MagicMock
from selenium.webdriver.common.keys import Keys
from ..email_providers.outlook import create_account
from ..utils.webdriver_utils import create_driver

@pytest.fixture(autouse=True)
def mock_outlook(monkeypatch):
# Mock GeckoDriverManager and ChromeDriverManager installations
monkeypatch.setattr('webdriver_manager.firefox.GeckoDriverManager.install', lambda *args, **kwargs: 'gecko_driver_path')
monkeypatch.setattr('webdriver_manager.chrome.ChromeDriverManager.install', lambda *args, **kwargs: 'chrome_driver_path')

# Mock WebDriver classes
mock_firefox = Mock()
mock_chrome = Mock()
monkeypatch.setattr('selenium.webdriver.Firefox', lambda *args, **kwargs: mock_firefox)
monkeypatch.setattr('selenium.webdriver.Chrome', lambda *args, **kwargs: mock_chrome)

# Mock WebDriver methods
mock_firefox.get = MagicMock()
mock_chrome.get = MagicMock()
mock_firefox.find_element = MagicMock(return_value=Mock())
mock_chrome.find_element = MagicMock(return_value=Mock())
mock_firefox.find_elements = MagicMock(return_value=[Mock()])
mock_chrome.find_elements = MagicMock(return_value=[Mock()])
mock_firefox.quit = MagicMock()
mock_chrome.quit = MagicMock()

# Mock WebDriverWait and expected_conditions
mock_wait = MagicMock()
monkeypatch.setattr('selenium.webdriver.support.ui.WebDriverWait', lambda *args, **kwargs: mock_wait)
mock_wait.until = MagicMock(return_value=Mock())
monkeypatch.setattr('selenium.webdriver.support.expected_conditions.presence_of_element_located', lambda *args, **kwargs: MagicMock())
monkeypatch.setattr('selenium.webdriver.support.expected_conditions.element_to_be_clickable', lambda *args, **kwargs: MagicMock())
monkeypatch.setattr('selenium.webdriver.support.expected_conditions.visibility_of_element_located', lambda *args, **kwargs: MagicMock())
monkeypatch.setattr('selenium.webdriver.support.expected_conditions.frame_to_be_available_and_switch_to_it', lambda *args, **kwargs: MagicMock())

# Correctly mock Select class
def mock_select_init(self, *args, **kwargs):
self.select_by_index = MagicMock()
self.select_by_visible_text = MagicMock()
self.select_by_value = MagicMock()

monkeypatch.setattr('selenium.webdriver.support.ui.Select.__init__', mock_select_init)

# Mock Keys
monkeypatch.setattr('selenium.webdriver.common.keys.Keys', Keys)

# Mock time
monkeypatch.setattr('time.sleep', MagicMock(()))

# Mock logging
monkeypatch.setattr('logging.info', MagicMock())
monkeypatch.setattr('logging.error', MagicMock())

# Mock sms services
def mock_get_phone_getsmscode(self, *args, **kwargs):
return '111111111'

def mock_get_phone_smspool(self, *args, **kwargs):
return 'ordeid', '111111111'

def mock_get_code(self, *arg, **kwargs):
return '000000'

def mock_get_phone_fail(self, *args, **kwargs):
raise Exception("Failed to get phone number")

def mock_get_code_fail(self, *args, **kwargs):
raise Exception("Failed to retreive code")

def test_create_account_firefox():

driver = create_driver('firefox')
# Test data
captcha_key= "4564654777"
username = "testuser"
password = "testpassword"
first_name = "John"
last_name = "Doe"
country = "us"
month = "1"
day = "1"
year = "2000"
hotmail = False

email, password = create_account(captcha_key, driver, username, password, first_name, last_name, country, month, day, year, hotmail)
assert email == f"{username}@outlook.com"
assert password == "testpassword"

def test_create_account_chrome():

driver = create_driver('chrome')
# Test data
captcha_key="5456646656"
username = "testuser"
password = "testpassword"
first_name = "John"
last_name = "Doe"
country="us"
month = "1"
day = "1"
year = "2000"
hotmail = False

email, password = create_account(captcha_key, driver, username, password, first_name, last_name, country, month, day, year, hotmail)
assert email == f"{username}@outlook.com"
assert password == "testpassword"

def test_create_hotmail_account():

driver = create_driver('chrome')
# Test data

captcha_key="5456646656"
username = "testuser"
password = "testpassword"
first_name = "John"
last_name = "Doe"
country = "us"
month = "1"
day = "1"
year = "2000"
hotmail = True

email, password = create_account(captcha_key, driver, username, password, first_name, last_name, country, month, day, year, hotmail)
assert email == f"{username}@hotmail.com"
assert password == "testpassword"

Loading

0 comments on commit 873bd15

Please sign in to comment.