Skip to content

Commit

Permalink
fix: github workflow test issues fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
RawanMostafa08 committed Oct 20, 2024
1 parent c9d3916 commit e70cdfc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
7 changes: 5 additions & 2 deletions SecretNote/accounts/test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.test import TestCase,Client
from django.contrib.auth import get_user_model
from django.utils import timezone
class TestSignup(TestCase):
def test_signup_rate_limited(self):
self.client = Client(REMOTE_ADDR='192.168.0.1')
Expand All @@ -18,13 +17,14 @@ def test_signup_rate_limited(self):

response = self.client.post('/accounts/signup/',
data={
'username': 'test_user22',
'username': 'test_user26',
'password1': 'strong_password',
'password2': 'strong_password',
})
self.assertEqual(response.status_code,403)

def test_signup_correctly(self):
self.client = Client(REMOTE_ADDR='192.127.0.1')
data = {
'username': 'test_user',
'password1': 'strong_password',
Expand All @@ -36,6 +36,7 @@ def test_signup_correctly(self):
self.assertIsNotNone(user)

def test_signup_incorrectly(self):
self.client = Client(REMOTE_ADDR='192.127.0.1')
data = {
'username': 'test_user',
'password1': 'strong_passworc',
Expand All @@ -52,6 +53,7 @@ def test_signup_incorrectly(self):

class TestLogin(TestCase):
def test_login_correctly(self):
self.client = Client(REMOTE_ADDR='192.127.0.1')
user = get_user_model().objects.create_user(username='test_user', password='strong_password')
data = {
'username': 'test_user',
Expand All @@ -63,6 +65,7 @@ def test_login_correctly(self):
self.assertTrue(response.context['user'].is_authenticated)

def test_login_incorrectly(self):
self.client = Client(REMOTE_ADDR='192.127.0.1')
user = get_user_model().objects.create_user(username='test_user', password='strong_password')
data = {
'username': 'test_user',
Expand Down
1 change: 0 additions & 1 deletion SecretNote/accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@

urlpatterns = [
path("signup/", ratelimit(key='ip', rate='20/m', method='POST', block=True) (SignUpView.as_view()), name="signup"),

]
30 changes: 9 additions & 21 deletions SecretNote/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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 @@ -29,39 +28,33 @@ class EndToEndTests(StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
options = Options()
options.headless = True
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-gpu")

cls.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
cls.driver.implicitly_wait(10)
cls.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
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"

def signup(self,username,password1,password2):
self.driver.get(f"{self.live_server_url}/accounts/signup")
@classmethod
def signup(cls,username,password1,password2):
cls.driver.get(f"{cls.live_server_url}/accounts/signup")

username_field=self.driver.find_element(By.ID,"id_username")
username_field=cls.driver.find_element(By.ID,"id_username")
username_field.send_keys(username)

password1_field=self.driver.find_element(By.ID,"id_password1")
password1_field=cls.driver.find_element(By.ID,"id_password1")
password1_field.send_keys(password1)

password2_field=self.driver.find_element(By.ID,"id_password2")
password2_field=cls.driver.find_element(By.ID,"id_password2")
password2_field.send_keys(password2)


button=self.driver.find_element(By.CLASS_NAME,"submit-btn")
button=cls.driver.find_element(By.CLASS_NAME,"submit-btn")
button.click()

def login(self,username,password):
Expand Down Expand Up @@ -100,21 +93,18 @@ def test_correct_signup(self):
self.signup(self.username,self.password,self.password)
assert "/login" in self.driver.current_url
assert is_element_present_by_id(self.driver,"login-title") is True
self.driver.close()

def test_incorrect_signup(self):
self.signup(self.username,self.password,"another_password")
assert "/login" not in self.driver.current_url
assert is_element_present_by_id(self.driver,"error") is True
self.driver.close()

def test_correct_login(self):
self.signup(self.username,self.password,self.password)
self.login(self.username,self.password)
assert "/notes" in self.driver.current_url
assert is_element_present_by_id(self.driver,"create-note-btn") is True

self.driver.close()

def test_incorrect_username_login(self):
self.signup(self.username,self.password,self.password)
Expand All @@ -123,7 +113,6 @@ def test_incorrect_username_login(self):
assert "/notes" not in self.driver.current_url
assert "/login" in self.driver.current_url
assert is_element_present_by_id(self.driver,"create-note-btn") is False
self.driver.close()

def test_incorrect_password_login(self):
self.signup(self.username,self.password,self.password)
Expand All @@ -132,7 +121,6 @@ def test_incorrect_password_login(self):
assert "/notes" not in self.driver.current_url
assert "/login" in self.driver.current_url
assert is_element_present_by_id(self.driver,"create-note-btn") is False
self.driver.close()

def test_correct_create_note(self):
self.signup(self.username,self.password,self.password)
Expand Down

0 comments on commit e70cdfc

Please sign in to comment.