Beispiel #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)
Beispiel #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)
Beispiel #3
0
    def test_login_senha_errada(self):
        user = User(
            username='******',
            password=
            '******',
            email='*****@*****.**')
        user.save()

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

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

        with self.assertRaises(RequirementsNotMet):
            Usuario().registrar('u', '1234', '1234', '*****@*****.**')
Beispiel #7
0
    def test_registrar_senhas_diferentes(self):
        user = User(username='******', password='******', email='*****@*****.**')
        user.save()

        with self.assertRaises(RequirementsNotMet):
            Usuario().registrar('user2', '1234', '4321', '*****@*****.**')
Beispiel #8
0
    def test_registrar_email_invalido(self):
        user = User(username='******', password='******', email='*****@*****.**')
        user.save()

        with self.assertRaises(InvalidEmail):
            Usuario().registrar('user2', '1234', '1234', 'invalid-email')
Beispiel #9
0
    def test_registrar_email_ja_existe(self):
        user = User(username='******', password='******', email='*****@*****.**')
        user.save()

        with self.assertRaises(UsernameOrEmailAlreadyExistis):
            Usuario().registrar('user2', '1234', '1234', '*****@*****.**')
Beispiel #10
0
class TestInscricao(unittest.TestCase):
    def setUp(self):
        logging.disable(logging.CRITICAL)
        connect('mongoenginetest', host='mongomock://localhost')
        self.parlamentar1 = Parlamentar(id='id', cargo='BR1')
        self.parlamentar2 = Parlamentar(id='id', cargo='BR2')
        self.parlamentar3 = Parlamentar(id='di', cargo='BR1')

    def tearDown(self):
        User.drop_collection()

    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_obter_todas_inscricoes_para_processar_semana_par(self):
        data_final = datetime(2019, 10, 19)
        self._prepare_tests_obter_todas_inscricoes_para_processar()

        actual = Inscricao().obter_todas_inscricoes_para_processar(
            data_final=data_final)

        self.assertEqual(actual, [self.user1, self.user2])

    def test_obter_todas_inscricoes_para_processar_semana_impar(self):
        data_final = datetime(2019, 10, 12)
        self._prepare_tests_obter_todas_inscricoes_para_processar()

        actual = Inscricao().obter_todas_inscricoes_para_processar(
            data_final=data_final)

        self.assertEqual(actual, [self.user1])

    def test_obter_todas_inscricoes_para_processar_semana_de_mes(self):
        data_final = datetime(2019, 11, 2)
        self._prepare_tests_obter_todas_inscricoes_para_processar()

        actual = Inscricao().obter_todas_inscricoes_para_processar(
            data_final=data_final)

        self.assertEqual(actual, [self.user1, self.user2, self.user4])

    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 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 test_obter_minhas_inscricoes_user_doesnt_exist(self):
        with self.assertRaises(UserDoesNotExist):
            Inscricao().obter_minhas_inscricoes('*****@*****.**')

    @patch("legislei.services.inscricoes.obter_parlamentar")
    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]))

    @patch("legislei.services.inscricoes.obter_parlamentar")
    def test_nova_inscricao_outra_inscricao(self, mock_obter_parlamentar):
        warnings.simplefilter("ignore")
        mock_obter_parlamentar.return_value = self.parlamentar1
        User(username='******',
             password='******',
             email='*****@*****.**',
             inscricoes=Inscricoes(intervalo=14,
                                   parlamentares=[self.parlamentar2])).save()

        Inscricao().nova_inscricao(self.parlamentar1.cargo,
                                   self.parlamentar1.id, '*****@*****.**')

        self.assertEqual(
            User.objects(username='******').first().inscricoes,
            Inscricoes(intervalo=14,
                       parlamentares=[self.parlamentar2, self.parlamentar1]))

    def test_nova_inscricao_user_doesnt_exist(self):
        with self.assertRaises(UserDoesNotExist):
            Inscricao().nova_inscricao(self.parlamentar1.cargo,
                                       self.parlamentar1.id, '*****@*****.**')

    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 test_alterar_configs_valida(self):
        User(username='******',
             password='******',
             email='*****@*****.**',
             inscricoes=Inscricoes(intervalo=7, parlamentares=[])).save()

        Inscricao().alterar_configs(21, '*****@*****.**')

        self.assertEqual(
            User.objects(username='******').first().inscricoes.intervalo, 21)

    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)