Passed
Pull Request — develop (#29)
by inkhey
02:41
created

tracim_backend.tests.functional.test_reset_password   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 168
Duplicated Lines 62.5 %

Importance

Changes 0
Metric Value
wmc 7
eloc 136
dl 105
loc 168
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A TestResetPasswordRequestEndpointMailSync.test_api__reset_password_request__ok__nominal_case() 0 18 1
A TestResetPasswordCheckTokenEndpoint.test_api__reset_password_check_token__err_400__invalid_token() 20 20 1
A TestResetPasswordCheckTokenEndpoint.test_api__reset_password_check_token__ok_204__nominal_case() 20 20 1
A TestResetPasswordRequestEndpointMailSync.test_api__reset_password_request__err_400__user_not_exist() 0 14 1
A TestResetPasswordModifyEndpoint.test_api__reset_password_reset__ok_204__nominal_case() 22 22 1
A TestResetPasswordModifyEndpoint.test_api__reset_password_reset__err_400__password_does_not_match() 22 22 1
A TestResetPasswordModifyEndpoint.test_api__reset_password_reset__err_400__invalid_token() 21 21 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import requests
2
import transaction
3
from tracim_backend import models
4
from tracim_backend.models import get_tm_session
5
from tracim_backend.tests import FunctionalTest
6
from tracim_backend.fixtures.users_and_groups import Base as BaseFixture
7
from tracim_backend.lib.core.user import UserApi
8
9
10
class TestResetPasswordRequestEndpointMailSync(FunctionalTest):
11
12
    fixtures = [BaseFixture]
13
    config_section = 'functional_test_with_mail_test_sync'
14
15
    def test_api__reset_password_request__ok__nominal_case(self):
16
        requests.delete('http://127.0.0.1:8025/api/v1/messages')
17
        params = {
18
            'email': '[email protected]'
19
        }
20
        self.testapp.post_json(
21
            '/api/v2/reset_password/request',
22
            status=204,
23
            params=params,
24
        )
25
        response = requests.get('http://127.0.0.1:8025/api/v1/messages')
26
        response = response.json()
27
        assert len(response) == 1
28
        headers = response[0]['Content']['Headers']
29
        assert headers['From'][0] == 'Tracim Notifications <test_user_from+0@localhost>'  # nopep8
30
        assert headers['To'][0] == 'Global manager <[email protected]>'
31
        assert headers['Subject'][0] == '[TRACIM] Reset Password Request'
32
        requests.delete('http://127.0.0.1:8025/api/v1/messages')
33
34
    def test_api__reset_password_request__err_400__user_not_exist(self):
35
        requests.delete('http://127.0.0.1:8025/api/v1/messages')
36
        params = {
37
            'email': '[email protected]'
38
        }
39
        self.testapp.post_json(
40
            '/api/v2/reset_password/request',
41
            status=400,
42
            params=params,
43
        )
44
        response = requests.get('http://127.0.0.1:8025/api/v1/messages')
45
        response = response.json()
46
        assert len(response) == 0
47
        requests.delete('http://127.0.0.1:8025/api/v1/messages')
48
49
50
class TestResetPasswordCheckTokenEndpoint(FunctionalTest):
51
52 View Code Duplication
    def test_api__reset_password_check_token__ok_204__nominal_case(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
53
        dbsession = get_tm_session(self.session_factory, transaction.manager)
54
        admin = dbsession.query(models.User) \
55
            .filter(models.User.email == '[email protected]') \
56
            .one()
57
        uapi = UserApi(
58
            current_user=admin,
59
            session=dbsession,
60
            config=self.app_config,
61
        )
62
        reset_password_token = uapi.reset_password_notification(admin, do_save=True) # nopep8
63
        transaction.commit()
64
        params = {
65
            'email': '[email protected]',
66
            'reset_password_token': reset_password_token
67
        }
68
        self.testapp.post_json(
69
            '/api/v2/reset_password/check_token',
70
            status=204,
71
            params=params,
72
        )
73
74 View Code Duplication
    def test_api__reset_password_check_token__err_400__invalid_token(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
75
        dbsession = get_tm_session(self.session_factory, transaction.manager)
76
        admin = dbsession.query(models.User) \
77
            .filter(models.User.email == '[email protected]') \
78
            .one()
79
        uapi = UserApi(
80
            current_user=admin,
81
            session=dbsession,
82
            config=self.app_config,
83
        )
84
        reset_password_token = 'wrong_token'
85
        transaction.commit()
86
        params = {
87
            'email': '[email protected]',
88
            'reset_password_token': reset_password_token
89
        }
90
        self.testapp.post_json(
91
            '/api/v2/reset_password/check_token',
92
            status=401,
93
            params=params,
94
        )
95
96
97
class TestResetPasswordModifyEndpoint(FunctionalTest):
98
99 View Code Duplication
    def test_api__reset_password_reset__ok_204__nominal_case(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
100
        dbsession = get_tm_session(self.session_factory, transaction.manager)
101
        admin = dbsession.query(models.User) \
102
            .filter(models.User.email == '[email protected]') \
103
            .one()
104
        uapi = UserApi(
105
            current_user=admin,
106
            session=dbsession,
107
            config=self.app_config,
108
        )
109
        reset_password_token = uapi.reset_password_notification(admin, do_save=True)  # nopep8
110
        transaction.commit()
111
        params = {
112
            'email': '[email protected]',
113
            'reset_password_token': reset_password_token,
114
            'new_password': 'mynewpassword',
115
            'new_password2': 'mynewpassword',
116
        }
117
        self.testapp.post_json(
118
            '/api/v2/reset_password/modify',
119
            status=204,
120
            params=params,
121
        )
122
123 View Code Duplication
    def test_api__reset_password_reset__err_400__invalid_token(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
124
        dbsession = get_tm_session(self.session_factory, transaction.manager)
125
        admin = dbsession.query(models.User) \
126
            .filter(models.User.email == '[email protected]') \
127
            .one()
128
        uapi = UserApi(
129
            current_user=admin,
130
            session=dbsession,
131
            config=self.app_config,
132
        )
133
        reset_password_token = 'wrong_token'
134
        params = {
135
            'email': '[email protected]',
136
            'reset_password_token': reset_password_token,
137
            'new_password': 'mynewpassword',
138
            'new_password2': 'mynewpassword',
139
        }
140
        self.testapp.post_json(
141
            '/api/v2/reset_password/modify',
142
            status=401,
143
            params=params,
144
        )
145
146 View Code Duplication
    def test_api__reset_password_reset__err_400__password_does_not_match(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
147
        dbsession = get_tm_session(self.session_factory, transaction.manager)
148
        admin = dbsession.query(models.User) \
149
            .filter(models.User.email == '[email protected]') \
150
            .one()
151
        uapi = UserApi(
152
            current_user=admin,
153
            session=dbsession,
154
            config=self.app_config,
155
        )
156
        reset_password_token = uapi.reset_password_notification(admin, do_save=True)  # nopep8
157
        transaction.commit()
158
        params = {
159
            'email': '[email protected]',
160
            'reset_password_token': reset_password_token,
161
            'new_password': 'mynewpassword',
162
            'new_password2': 'anotherpassword',
163
        }
164
        self.testapp.post_json(
165
            '/api/v2/reset_password/modify',
166
            status=400,
167
            params=params,
168
        )
169