Best Python code snippet using localstack_python
permissions.py
Source:permissions.py
1from rest_framework.permissions import BasePermission2from .authentication import *3class IsStudentAuthenticated(BasePermission):4 5 def has_permission(self, request, view):6 # accès aux utilisateurs étudiants authentifiés7 #Tester si l'utilisateur qui demande la page n'est pas un utilisatreur anonyme8 if request.user.is_anonymous:9 return bool(False)10 else:11 token, _ = Token.objects.get_or_create(user = request.user)12 is_expired = is_token_expired(token) 13 if(is_expired):14 token.delete()15 request.user.is_authenticated=False16 return bool(False)17 else:18 return bool(request.user and request.user.is_authenticated and request.user.has_perm("eptGSI.is_student") and not is_expired)19class IsMaitreStageAuthenticated(BasePermission):20 21 def has_permission(self, request, view):22 # accès aux utilisateurs maitres de stage authentifiés23 #Tester si l'utilisateur qui demande la page n'est pas un utilisatreur anonyme24 if request.user.is_anonymous:25 return bool(False)26 else:27 token, _ = Token.objects.get_or_create(user = request.user)28 is_expired = is_token_expired(token) 29 print(is_expired)30 if(is_expired):31 token.delete()32 request.user.is_authenticated=False33 return bool(False)34 else:35 return bool(request.user and request.user.is_authenticated and request.user.has_perm("eptGSI.is_maitre_stage") and not is_expired)36class IsFormateurAuthenticated(BasePermission):37 38 def has_permission(self, request, view):39 # accès aux utilisateurs formateurs authentifiés40 #Tester si l'utilisateur qui demande la page n'est pas un utilisatreur anonyme41 if request.user.is_anonymous:42 return bool(False)43 else:44 token, _ = Token.objects.get_or_create(user = request.user)45 is_expired = is_token_expired(token) 46 print(is_expired)47 if(is_expired):48 token.delete()49 request.user.is_authenticated=False50 return bool(False)51 else:52 return bool(request.user and request.user.is_authenticated and request.user.has_perm("eptGSI.is_formateur") and not is_expired)53 54 55class IsManagerAuthenticated(BasePermission):56 57 def has_permission(self, request, view):58 # accès aux utilisateurs managers authentifiés59 #Tester si l'utilisateur qui demande la page n'est pas un utilisatreur anonyme60 if request.user.is_anonymous:61 return bool(False)62 else:63 token, _ = Token.objects.get_or_create(user = request.user)64 is_expired = is_token_expired(token) 65 print(is_expired)66 if(is_expired):67 token.delete()68 request.user.is_authenticated=False69 return bool(False)70 else:71 return bool(request.user and request.user.is_authenticated and request.user.has_perm("eptGSI.is_manager") and not is_expired)72class IsResponsableImmersionAuthenticated(BasePermission):73 74 def has_permission(self, request, view):75 # accès aux utilisateurs responsables des immersions authentifiés76 #Tester si l'utilisateur qui demande la page n'est pas un utilisatreur anonyme77 if request.user.is_anonymous:78 return bool(False)79 else:80 token, _ = Token.objects.get_or_create(user = request.user)81 is_expired = is_token_expired(token) 82 print(is_expired)83 if(is_expired):84 token.delete()85 request.user.is_authenticated=False86 return bool(False)87 else:...
test_timer.py
Source:test_timer.py
1"""Unit tests for the Timer class."""2import logging3import time4import unittest5from pynetdicom3.timer import Timer6LOGGER = logging.getLogger('pynetdicom3')7LOGGER.setLevel(logging.CRITICAL)8class TestTimer(unittest.TestCase):9 """Test the Timer class."""10 def test_init(self):11 """Test Timer initialisation"""12 timer = Timer(10)13 self.assertTrue(timer.timeout_seconds == 10)14 timer = Timer(0)15 self.assertTrue(timer.timeout_seconds == 0)16 timer = Timer(None)17 self.assertTrue(timer.timeout_seconds is None)18 def test_property_setters_getters(self):19 """Test Timer property setters and getters."""20 timer = Timer(0)21 self.assertEqual(timer.timeout_seconds, 0)22 self.assertFalse(timer.is_expired)23 self.assertEqual(timer.time_remaining, 0)24 timer = Timer(None)25 self.assertEqual(timer.timeout_seconds, None)26 self.assertFalse(timer.is_expired)27 self.assertEqual(timer.time_remaining, -1)28 timer.timeout_seconds = 1029 self.assertEqual(timer.timeout_seconds, 10)30 self.assertFalse(timer.is_expired)31 self.assertEqual(timer.time_remaining, 10)32 timer.timeout_seconds = 0.233 timer.start()34 time.sleep(0.1)35 self.assertTrue(timer.time_remaining < 0.1)36 self.assertFalse(timer.is_expired)37 time.sleep(0.1)38 self.assertTrue(timer.is_expired)39 timer.timeout_seconds = None40 self.assertEqual(timer.timeout_seconds, None)41 def test_start_stop(self):42 """Test Timer stops."""43 timer = Timer(0.2)44 timer.start()45 time.sleep(0.1)46 timer.stop()47 time.sleep(0.2)48 self.assertFalse(timer.is_expired)49 def test_restart(self):50 """Test Timer restarts correctly."""51 timer = Timer(0.2)52 timer.start()53 time.sleep(0.1)54 timer.restart()55 time.sleep(0.15)56 self.assertFalse(timer.is_expired)57 time.sleep(0.05)58 self.assertTrue(timer.is_expired)59if __name__ == "__main__":...
token_test.py
Source:token_test.py
1import pytest2from time import time3from vaillant_netatmo_api.token import Token4@pytest.mark.asyncio5class TestToken:6 async def test_serialize__json_token_object__returns_json_string(self):7 token = Token({8 "access_token": "12345",9 "refresh_token": "abcde",10 "expires_at": 12,11 "expires_in": ""12 })13 expected_json = '{"access_token": "12345", "refresh_token": "abcde", "expires_at": 12}'14 json = token.serialize()15 assert json == expected_json16 async def test_deserialize__json_string__returns_json_token_object(self):17 json = '{"access_token": "12345", "refresh_token": "abcde", "expires_at": 12}'18 19 expected_token = Token({20 "access_token": "12345",21 "refresh_token": "abcde",22 "expires_at": 12,23 })24 token = Token.deserialize(json)25 assert isinstance(token, Token)26 assert token == expected_token27 async def test_is_expired__invalid_token__returns_true(self):28 token = Token({29 "access_token": "12345",30 "refresh_token": "abcde",31 "expires_at": "invalid",32 })33 is_expired = token.is_expired34 assert is_expired == True35 async def test_is_expired__expired_token__returns_true(self):36 token = Token({37 "access_token": "12345",38 "refresh_token": "abcde",39 "expires_at": int(time()) - 1000,40 })41 is_expired = token.is_expired42 assert is_expired == True43 async def test_is_expired__non_expired_token__returns_false(self):44 token = Token({45 "access_token": "12345",46 "refresh_token": "abcde",47 "expires_at": int(time()) + 1000,48 })49 is_expired = token.is_expired...
test_models.py
Source:test_models.py
1from datetime import timedelta2from unittest import TestCase3import pytest4from django.utils import timezone5from organisations.invites.models import InviteLink6from organisations.models import Organisation7@pytest.mark.django_db8class InviteLinkTestCase(TestCase):9 def setUp(self) -> None:10 self.organisation = Organisation.objects.create(name="Test organisation")11 def test_is_expired_expiry_date_in_past(self):12 # Given13 yesterday = timezone.now() - timedelta(days=1)14 expired_link = InviteLink.objects.create(15 organisation=self.organisation, expires_at=yesterday16 )17 # When18 is_expired = expired_link.is_expired19 # Then20 assert is_expired21 def test_is_expired_expiry_date_in_future(self):22 # Given23 tomorrow = timezone.now() + timedelta(days=1)24 expired_link = InviteLink.objects.create(25 organisation=self.organisation, expires_at=tomorrow26 )27 # When28 is_expired = expired_link.is_expired29 # Then30 assert not is_expired31 def test_is_expired_no_expiry_date(self):32 # Given33 expired_link = InviteLink.objects.create(34 organisation=self.organisation, expires_at=None35 )36 # When37 is_expired = expired_link.is_expired38 # Then...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!