diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml new file mode 100644 index 0000000..e4ff804 --- /dev/null +++ b/.github/workflows/main.yaml @@ -0,0 +1,31 @@ +name: Django CI/CD + +on: + push: + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.10' + + - name: Install Dependencies + run: | + python -m pip install --upgrade pip + cd SecretNote + pip install -r requirements.txt + cd .. + + - name: Run Tests + run: | + cd SecretNote + python manage.py test + cd .. diff --git a/SecretNote/accounts/test.py b/SecretNote/accounts/test.py index be2f35d..0b886ab 100644 --- a/SecretNote/accounts/test.py +++ b/SecretNote/accounts/test.py @@ -4,38 +4,21 @@ class TestSignup(TestCase): def test_signup_rate_limited(self): self.client = Client(REMOTE_ADDR='192.168.0.1') - data =[ { - 'username': 'test_user1', - 'password1': 'strong_password', - 'password2': 'strong_password', - }, - { - 'username': 'test_user2', - 'password1': 'strong_password', - 'password2': 'strong_password', - }, - { - 'username': 'test_user3', - 'password1': 'strong_password', - 'password2': 'strong_password', - }, - { - 'username': 'test_user4', - 'password1': 'strong_password', - 'password2': 'strong_password', - }, - { - 'username': 'test_user5', - 'password1': 'strong_password', - 'password2': 'strong_password', - }] - for i in range(5): + data = [ + { + 'username': f'test_user{i}', + 'password1': 'strong_password', + 'password2': 'strong_password', + } + for i in range(1, 21) + ] + for i in range(len(data)): response=self.client.post('/accounts/signup/', data=data[i]) self.assertEqual(response.status_code,302) response = self.client.post('/accounts/signup/', data={ - 'username': 'test_user6', + 'username': 'test_user22', 'password1': 'strong_password', 'password2': 'strong_password', }) diff --git a/SecretNote/accounts/urls.py b/SecretNote/accounts/urls.py index c498eb2..d1e9462 100644 --- a/SecretNote/accounts/urls.py +++ b/SecretNote/accounts/urls.py @@ -4,7 +4,6 @@ urlpatterns = [ - path("signup/", ratelimit(key='ip', rate='5/m', method='POST', block=True) (SignUpView.as_view()), name="signup"), - # path("signup/", SignUpView.as_view(), name="signup"), + path("signup/", ratelimit(key='ip', rate='20/m', method='POST', block=True) (SignUpView.as_view()), name="signup"), ] \ No newline at end of file diff --git a/SecretNote/tests.py b/SecretNote/tests.py index 623f5aa..a770e4a 100644 --- a/SecretNote/tests.py +++ b/SecretNote/tests.py @@ -1,9 +1,11 @@ -from django.test import LiveServerTestCase +from django.contrib.staticfiles.testing import StaticLiveServerTestCase from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.by import By +from django.conf import settings from selenium.common.exceptions import NoSuchElementException +from selenium.webdriver.chrome.options import Options import random import string @@ -23,7 +25,20 @@ def is_element_present_by_css(driver, css): return True -class EndToEndTests(LiveServerTestCase): +class EndToEndTests(StaticLiveServerTestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + options = Options() + options.add_argument("--headless") + cls.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) + cls.driver.implicitly_wait(1) + + @classmethod + def tearDownClass(cls): + cls.driver.quit() + super().tearDownClass() + def setUp(self): self.username = f"testinguser_{''.join(random.choices(string.ascii_lowercase,k=8))}" @@ -31,7 +46,7 @@ def setUp(self): self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) def signup(self,username,password1,password2): - self.driver.get('http://127.0.0.1:8000/accounts/signup') + self.driver.get(f"{self.live_server_url}/accounts/signup") username_field=self.driver.find_element(By.ID,"id_username") username_field.send_keys(username) @@ -47,7 +62,7 @@ def signup(self,username,password1,password2): button.click() def login(self,username,password): - self.driver.get('http://127.0.0.1:8000/accounts/login') + self.driver.get(f"{self.live_server_url}/accounts/login") username_field=self.driver.find_element(By.ID,"id_username") username_field.send_keys(username) @@ -124,7 +139,7 @@ def test_correct_create_note(self): self.create_note("test-title","test-content","12/10/2024",2) - assert "http://127.0.0.1:8000/notes/" == self.driver.current_url + assert "create" not in self.driver.current_url assert is_element_present_by_id(self.driver,"no_notes") is False latest_note_title=self.driver.find_elements(By.CLASS_NAME,"title")