예제 #1
0
    def test_obter_por_id(self):
        user = User(username='******', password='******', email='*****@*****.**')
        user.save()

        actual = Usuario().obter_por_id(user.pk)

        self.assertEqual(actual, user)
예제 #2
0
    def test_login_usuario_nao_existe(self):
        user = User(username='******', password='******', email='*****@*****.**')
        user.save()

        actual = Usuario().login('inexistente', '4321', True)

        self.assertFalse(actual)
예제 #3
0
    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)
예제 #4
0
 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()
예제 #5
0
    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]))
예제 #6
0
    def test_login_senha_errada(self):
        user = User(
            username='******',
            password=
            '******',
            email='*****@*****.**')
        user.save()

        actual = Usuario().login('user', '4321', True)

        self.assertFalse(actual)
예제 #7
0
    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)
예제 #8
0
    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=[]))
예제 #9
0
 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
예제 #10
0
    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)
예제 #11
0
 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()
예제 #12
0
    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, [])
예제 #13
0
 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)
예제 #14
0
 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)
예제 #15
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))
예제 #16
0
 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
예제 #17
0
 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
예제 #18
0
    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])
예제 #19
0
    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")
            ])
예제 #20
0
 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)
예제 #21
0
 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"])
예제 #22
0
 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())
예제 #23
0
    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")
예제 #24
0
    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")
            ])
예제 #25
0
 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)
예제 #26
0
    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)
예제 #27
0
 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
예제 #28
0
 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)
예제 #29
0
 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)
예제 #30
0
    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",
                )
            ])