Skip to content

Commit

Permalink
chore: github workflow file, fixing the tests to use the ststic live …
Browse files Browse the repository at this point in the history
…server, increasing the rate limit for testing purposes
  • Loading branch information
RawanMostafa08 committed Oct 20, 2024
1 parent f787703 commit 28893f3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 34 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -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 ..
37 changes: 10 additions & 27 deletions SecretNote/accounts/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})
Expand Down
3 changes: 1 addition & 2 deletions SecretNote/accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),

]
25 changes: 20 additions & 5 deletions SecretNote/tests.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -23,15 +25,28 @@ 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))}"
self.password = "strong_password"
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)
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 28893f3

Please sign in to comment.