def test_obter_por_id(self): user = User(username='******', password='******', email='*****@*****.**') user.save() actual = Usuario().obter_por_id(user.pk) self.assertEqual(actual, user)
def test_login_usuario_nao_existe(self): user = User(username='******', password='******', email='*****@*****.**') user.save() actual = Usuario().login('inexistente', '4321', True) self.assertFalse(actual)
def test_alterar_configs_invalida(self): User(username='******', password='******', email='*****@*****.**', inscricoes=Inscricoes(intervalo=7, parlamentares=[])).save() Inscricao().alterar_configs(30, '*****@*****.**') self.assertEqual( User.objects(username='******').first().inscricoes.intervalo, 7)
def _prepare_tests_obter_todas_inscricoes_para_processar(self): inscricoes1 = Inscricoes( intervalo=7, parlamentares=[self.parlamentar1, self.parlamentar2]) inscricoes2 = Inscricoes(intervalo=14, parlamentares=[self.parlamentar1]) inscricoes3 = Inscricoes(intervalo=7, parlamentares=[]) inscricoes4 = Inscricoes(intervalo=28, parlamentares=[self.parlamentar3]) self.user1 = User(username='******', password='******', email='*****@*****.**', inscricoes=inscricoes1) self.user1.save() self.user2 = User(username='******', password='******', email='*****@*****.**', inscricoes=inscricoes2) self.user2.save() self.user3 = User(username='******', password='******', email='*****@*****.**', inscricoes=inscricoes3) self.user3.save() self.user4 = User(username='******', password='******', email='*****@*****.**', inscricoes=inscricoes4) self.user4.save() self.user5 = User(username='******', password='******', email='*****@*****.**').save()
def test_nova_inscricao_primeira_inscricao(self, mock_obter_parlamentar): warnings.simplefilter("ignore") mock_obter_parlamentar.return_value = self.parlamentar1 User(username='******', password='******', email='*****@*****.**').save() Inscricao().nova_inscricao(self.parlamentar1.cargo, self.parlamentar1.id, '*****@*****.**') self.assertEqual( User.objects(username='******').first().inscricoes, Inscricoes(intervalo=7, parlamentares=[self.parlamentar1]))
def test_login_senha_errada(self): user = User( username='******', password= '******', email='*****@*****.**') user.save() actual = Usuario().login('user', '4321', True) self.assertFalse(actual)
def test_login_sucesso(self, mock_login): user = User( username='******', password= '******', email='*****@*****.**') user.save() actual = Usuario().login('user', '1234', True) self.assertTrue(actual) mock_login.assert_called_once_with(user, remember=True)
def test_remover_inscricao(self): User(username='******', password='******', email='*****@*****.**', inscricoes=Inscricoes(intervalo=14, parlamentares=[self.parlamentar2])).save() Inscricao().remover_inscricao(self.parlamentar2.cargo, self.parlamentar2.id, '*****@*****.**') self.assertEqual( User.objects(username='******').first().inscricoes, Inscricoes(intervalo=14, parlamentares=[]))
def login(self, user_name, user_psw, remember_me): user_name = user_name.lower() user = User.objects(username=user_name).first() if user and pbkdf2_sha256.verify(user_psw, user.password): login_user(user, remember=remember_me) return True return False
def test_obter_minhas_inscricoes_nenhuma_inscricao(self): User(username='******', password='******', email='*****@*****.**').save() actual = Inscricao().obter_minhas_inscricoes('*****@*****.**') self.assertEqual(actual[0], []) self.assertEqual(actual[1], 7)
def verify_auth_token(self, token): s = Serializer(os.environ.get('APP_SECRET_KEY')) try: data = s.loads(token) except (BadSignature, SignatureExpired): return None return User.objects(pk=data['id']).first()
def test_obter_dispositivos_de_usuario_sem_devices(self): user = User(username="******", email="*****@*****.**", password="******").save() actual = Dispositivo().obter_dispositivos_de_usuario(user.id) self.assertEqual(actual, [])
def test_patch_dispositivos_sem_login(self): user = User.objects(username="******").first() actual = self.app.patch("/v1/usuarios/dispositivos/14", data=json.dumps({"active": False}), content_type='application/json') actual_data = json.loads(actual.data.decode("utf-8")) self.assertEqual(actual.status_code, 401) self.assertIn("message", actual_data)
def test_delete_dispositivos_sucesso(self): login_header = login_api(self.app, "test", "123") user = User.objects(username="******").first() actual = self.app.delete("/v1/usuarios/dispositivos/14", headers=login_header) actual_data = json.loads(actual.data.decode("utf-8")) self.assertEqual(actual.status_code, 200) self.assertEqual("Apagado", actual_data["message"]) self.assertEqual(len(user.reload().devices), 0)
def test_get_dispositivos_sucesso(self): login_header = login_api(self.app, "test", "123") user = User.objects(username="******").first() actual = self.app.get("/v1/usuarios/dispositivos", headers=login_header) actual_data = json.loads(actual.data.decode("utf-8")) self.assertEqual(actual.status_code, 200) self.assertIn("dispositivo", str(actual_data)) self.assertIn("---token---", str(actual_data))
def obter_minhas_inscricoes(self, email): user = User.objects(email=email) if not (user): raise UserDoesNotExist() inscricoes = user.first().inscricoes if inscricoes: return inscricoes.parlamentares, inscricoes.intervalo else: return [], 7
def registrar(self, nome, senha, senha_confirmada, email): user_name = nome.lower() if not (len(user_name) > 3 and len(senha) and senha == senha_confirmada): raise RequirementsNotMet() user = User(username=user_name, password=pbkdf2_sha256.using(rounds=16, salt_size=16).hash(senha), email=email) try: user.save() # Idealmente essa camada não deveria precisar conhecer exceptions do mongoengine? except NotUniqueError: raise UsernameOrEmailAlreadyExistis() except ValidationError: raise InvalidEmail() except Exception as e: raise e
def test_apagar_dispositivo(self): device1 = UserDevice(id="1", token="---token1---", active=False, name="Dispositivo1", os="Android 14") device2 = UserDevice(id="2", token="---token2---", name="Dispositivo2", os="Android 14") user = User(username="******", email="*****@*****.**", password="******", devices=[device1, device2]).save() Dispositivo().apagar_dispositivo(user.id, device1.id) self.assertEqual(User.objects(pk=user.id).first().devices, [device2])
def test_adicionar_dispositivo_sucesso(self): user = User(username="******", email="*****@*****.**", password="******").save() Dispositivo().adicionar_dispostivo(user_id=user.id, uuid="123", token="---token---", name="nome", os="os") self.assertEqual( User.objects(pk=user.id).first().devices, [ UserDevice(id="123", token="---token---", active=True, name="nome", os="os") ])
def test_patch_dispositivos_parametro_invalido(self): login_header = login_api(self.app, "test", "123") user = User.objects(username="******").first() actual = self.app.patch("/v1/usuarios/dispositivos/14", data=json.dumps({"active": "invalido"}), content_type='application/json', headers=login_header) actual_data = json.loads(actual.data.decode("utf-8")) self.assertEqual(actual.status_code, 400) self.assertIn("message", actual_data)
def test_patch_dispositivos_dispositivo_inexistente(self): login_header = login_api(self.app, "test", "123") user = User.objects(username="******").first() actual = self.app.patch("/v1/usuarios/dispositivos/124578", data=json.dumps({"active": False}), content_type='application/json', headers=login_header) actual_data = json.loads(actual.data.decode("utf-8")) self.assertEqual(actual.status_code, 400) self.assertEqual("Dispositivo não existe", actual_data["message"])
def test_patch_dispositivos_parametro_inexistente(self): login_header = login_api(self.app, "test", "123") user = User.objects(username="******").first() actual = self.app.patch("/v1/usuarios/dispositivos/14", data=json.dumps({"naoexisto": True}), content_type='application/json', headers=login_header) actual_data = json.loads(actual.data.decode("utf-8")) self.assertEqual(actual.status_code, 200) self.assertIn("message", actual_data) self.assertNotIn("naoexisto", user.reload().devices[0].to_json())
def test_adicionar_dispositivo_valores_invalidos(self): user = User(username="******", email="*****@*****.**", password="******").save() with self.assertRaises(InvalidParametersError): Dispositivo().adicionar_dispostivo(user_id=user.id, uuid=True, token="---token---", active="olar", name="nome", os="os")
def test_atualizar_dispositivo_sucesso(self): device = UserDevice(id="14", token="---token---", name="Dispositivo", os="Android 14") user = User(username="******", email="*****@*****.**", password="******", devices=[device]).save() Dispositivo().atualizar_dispositivo(user.id, device.id, active=False) self.assertEqual( User.objects(pk=user.id).first().devices, [ UserDevice(id="14", token="---token---", active=False, name="Dispositivo", os="Android 14") ])
def test_patch_dispositivos_sucesso(self): login_header = login_api(self.app, "test", "123") user = User.objects(username="******").first() actual = self.app.patch("/v1/usuarios/dispositivos/14", data=json.dumps({"active": False}), content_type='application/json', headers=login_header) actual_data = json.loads(actual.data.decode("utf-8")) self.assertEqual(actual.status_code, 200) self.assertEqual("Ok", actual_data["message"]) self.assertFalse(user.reload().devices[0].active)
def test_obter_minhas_inscricoes_com_inscricoes(self): inscricoes = Inscricoes( intervalo=14, parlamentares=[self.parlamentar1, self.parlamentar2]) User(username='******', password='******', email='*****@*****.**', inscricoes=inscricoes).save() actual = Inscricao().obter_minhas_inscricoes('*****@*****.**') self.assertEqual(actual[0], [self.parlamentar1, self.parlamentar2]) self.assertEqual(actual[1], 14)
def obter_todas_inscricoes_para_processar(self, data_final=datetime.now()): inscricoes = [] numero_semana = int(data_final.strftime("%V")) for user in User.objects(): if user.inscricoes and user.inscricoes.parlamentares != []: inscricao = user.inscricoes if (numero_semana % (inscricao.intervalo / 7) != 0): logging.info( "Pulando {} (intervalo de inscricao: {})".format( user.username, inscricao.intervalo)) else: inscricoes.append(user) return inscricoes
def test_post_dispositivos_faltando_parametros_obrigatorios(self): login_header = login_api(self.app, "test", "123") user_id = User.objects(username="******").first().pk actual = self.app.post("/v1/usuarios/dispositivos".format(user_id), data=json.dumps({ "uuid": "1414", "token": "---token2---", }), content_type='application/json', headers=login_header) actual_data = json.loads(actual.data.decode("utf-8")) self.assertEqual(actual.status_code, 400) self.assertIn("message", actual_data)
def test_post_dispositivos_sem_login(self): user_id = User.objects(username="******").first().pk actual = self.app.post("/v1/usuarios/dispositivos".format(user_id), data=json.dumps({ "uuid": "1414", "token": "---token2---", "name": "outro_dispositivo", "os": "iOS" }), content_type='application/json') actual_data = json.loads(actual.data.decode("utf-8")) self.assertEqual(actual.status_code, 401) self.assertIn("message", actual_data)
def test_atualizar_dispositivo_parametros_invalidos(self): device = UserDevice(id="14", token="---token---", name="Dispositivo", os="Android 14") user = User(username="******", email="*****@*****.**", password="******", devices=[device]).save() Dispositivo().atualizar_dispositivo(user.id, device.id, nao_existo=True) self.assertEqual( User.objects(pk=user.id).first().devices, [ UserDevice( id="14", token="---token---", name="Dispositivo", os="Android 14", ) ])