|
@@ -4,6 +4,7 @@ Tests for /api/v2/users subpath endpoints.
|
4
|
4
|
"""
|
5
|
5
|
from time import sleep
|
6
|
6
|
import pytest
|
|
7
|
+import requests
|
7
|
8
|
import transaction
|
8
|
9
|
|
9
|
10
|
from tracim_backend import models
|
|
@@ -2479,9 +2480,8 @@ class TestUserWorkspaceEndpoint(FunctionalTest):
|
2479
|
2480
|
|
2480
|
2481
|
|
2481
|
2482
|
class TestUserEndpoint(FunctionalTest):
|
2482
|
|
- # -*- coding: utf-8 -*-
|
2483
|
2483
|
"""
|
2484
|
|
- Tests for GET /api/v2/users/{user_id}
|
|
2484
|
+ Tests for GET/POST /api/v2/users/{user_id}
|
2485
|
2485
|
"""
|
2486
|
2486
|
fixtures = [BaseFixture]
|
2487
|
2487
|
|
|
@@ -2634,11 +2634,308 @@ class TestUserEndpoint(FunctionalTest):
|
2634
|
2634
|
status=403
|
2635
|
2635
|
)
|
2636
|
2636
|
|
|
2637
|
+ def test_api__create_user__ok_200__full_admin(self):
|
|
2638
|
+ self.testapp.authorization = (
|
|
2639
|
+ 'Basic',
|
|
2640
|
+ (
|
|
2641
|
+ 'admin@admin.admin',
|
|
2642
|
+ 'admin@admin.admin'
|
|
2643
|
+ )
|
|
2644
|
+ )
|
|
2645
|
+ params = {
|
|
2646
|
+ 'email': 'test@test.test',
|
|
2647
|
+ 'password': 'mysuperpassword',
|
|
2648
|
+ 'profile': 'users',
|
|
2649
|
+ 'timezone': 'Europe/Paris',
|
|
2650
|
+ 'public_name': 'test user',
|
|
2651
|
+ 'email_notification': False,
|
|
2652
|
+ }
|
|
2653
|
+ res = self.testapp.post_json(
|
|
2654
|
+ '/api/v2/users',
|
|
2655
|
+ status=200,
|
|
2656
|
+ params=params,
|
|
2657
|
+ )
|
|
2658
|
+ res = res.json_body
|
|
2659
|
+ assert res['user_id']
|
|
2660
|
+ user_id = res['user_id']
|
|
2661
|
+ assert res['created']
|
|
2662
|
+ assert res['is_active'] is True
|
|
2663
|
+ assert res['profile'] == 'users'
|
|
2664
|
+ assert res['email'] == 'test@test.test'
|
|
2665
|
+ assert res['public_name'] == 'test user'
|
|
2666
|
+ assert res['timezone'] == 'Europe/Paris'
|
|
2667
|
+
|
|
2668
|
+ dbsession = get_tm_session(self.session_factory, transaction.manager)
|
|
2669
|
+ admin = dbsession.query(models.User) \
|
|
2670
|
+ .filter(models.User.email == 'admin@admin.admin') \
|
|
2671
|
+ .one()
|
|
2672
|
+ uapi = UserApi(
|
|
2673
|
+ current_user=admin,
|
|
2674
|
+ session=dbsession,
|
|
2675
|
+ config=self.app_config,
|
|
2676
|
+ )
|
|
2677
|
+ user = uapi.get_one(user_id)
|
|
2678
|
+ assert user.email == 'test@test.test'
|
|
2679
|
+ assert user.validate_password('mysuperpassword')
|
|
2680
|
+
|
|
2681
|
+ def test_api__create_user__ok_200__limited_admin(self):
|
|
2682
|
+ self.testapp.authorization = (
|
|
2683
|
+ 'Basic',
|
|
2684
|
+ (
|
|
2685
|
+ 'admin@admin.admin',
|
|
2686
|
+ 'admin@admin.admin'
|
|
2687
|
+ )
|
|
2688
|
+ )
|
|
2689
|
+ params = {
|
|
2690
|
+ 'email': 'test@test.test',
|
|
2691
|
+ 'email_notification': False,
|
|
2692
|
+ }
|
|
2693
|
+ res = self.testapp.post_json(
|
|
2694
|
+ '/api/v2/users',
|
|
2695
|
+ status=200,
|
|
2696
|
+ params=params,
|
|
2697
|
+ )
|
|
2698
|
+ res = res.json_body
|
|
2699
|
+ assert res['user_id']
|
|
2700
|
+ user_id = res['user_id']
|
|
2701
|
+ assert res['created']
|
|
2702
|
+ assert res['is_active'] is True
|
|
2703
|
+ assert res['profile'] == 'users'
|
|
2704
|
+ assert res['email'] == 'test@test.test'
|
|
2705
|
+ assert res['public_name'] == 'test'
|
|
2706
|
+ assert res['timezone'] == ''
|
|
2707
|
+
|
|
2708
|
+ dbsession = get_tm_session(self.session_factory, transaction.manager)
|
|
2709
|
+ admin = dbsession.query(models.User) \
|
|
2710
|
+ .filter(models.User.email == 'admin@admin.admin') \
|
|
2711
|
+ .one()
|
|
2712
|
+ uapi = UserApi(
|
|
2713
|
+ current_user=admin,
|
|
2714
|
+ session=dbsession,
|
|
2715
|
+ config=self.app_config,
|
|
2716
|
+ )
|
|
2717
|
+ user = uapi.get_one(user_id)
|
|
2718
|
+ assert user.email == 'test@test.test'
|
|
2719
|
+ assert user.password
|
|
2720
|
+
|
|
2721
|
+ def test_api__create_user__err_400__email_already_in_db(self):
|
|
2722
|
+ dbsession = get_tm_session(self.session_factory, transaction.manager)
|
|
2723
|
+ admin = dbsession.query(models.User) \
|
|
2724
|
+ .filter(models.User.email == 'admin@admin.admin') \
|
|
2725
|
+ .one()
|
|
2726
|
+ uapi = UserApi(
|
|
2727
|
+ current_user=admin,
|
|
2728
|
+ session=dbsession,
|
|
2729
|
+ config=self.app_config,
|
|
2730
|
+ )
|
|
2731
|
+ gapi = GroupApi(
|
|
2732
|
+ current_user=admin,
|
|
2733
|
+ session=dbsession,
|
|
2734
|
+ config=self.app_config,
|
|
2735
|
+ )
|
|
2736
|
+ groups = [gapi.get_one_with_name('users')]
|
|
2737
|
+ test_user = uapi.create_user(
|
|
2738
|
+ email='test@test.test',
|
|
2739
|
+ password='pass',
|
|
2740
|
+ name='bob',
|
|
2741
|
+ groups=groups,
|
|
2742
|
+ timezone='Europe/Paris',
|
|
2743
|
+ do_save=True,
|
|
2744
|
+ do_notify=False,
|
|
2745
|
+ )
|
|
2746
|
+ uapi.save(test_user)
|
|
2747
|
+ transaction.commit()
|
|
2748
|
+ self.testapp.authorization = (
|
|
2749
|
+ 'Basic',
|
|
2750
|
+ (
|
|
2751
|
+ 'admin@admin.admin',
|
|
2752
|
+ 'admin@admin.admin'
|
|
2753
|
+ )
|
|
2754
|
+ )
|
|
2755
|
+ params = {
|
|
2756
|
+ 'email': 'test@test.test',
|
|
2757
|
+ 'password': 'mysuperpassword',
|
|
2758
|
+ 'profile': 'users',
|
|
2759
|
+ 'timezone': 'Europe/Paris',
|
|
2760
|
+ 'public_name': 'test user',
|
|
2761
|
+ 'email_notification': False,
|
|
2762
|
+ }
|
|
2763
|
+ res = self.testapp.post_json(
|
|
2764
|
+ '/api/v2/users',
|
|
2765
|
+ status=400,
|
|
2766
|
+ params=params,
|
|
2767
|
+ )
|
|
2768
|
+
|
|
2769
|
+ def test_api__create_user__err_403__other_user(self):
|
|
2770
|
+ dbsession = get_tm_session(self.session_factory, transaction.manager)
|
|
2771
|
+ admin = dbsession.query(models.User) \
|
|
2772
|
+ .filter(models.User.email == 'admin@admin.admin') \
|
|
2773
|
+ .one()
|
|
2774
|
+ uapi = UserApi(
|
|
2775
|
+ current_user=admin,
|
|
2776
|
+ session=dbsession,
|
|
2777
|
+ config=self.app_config,
|
|
2778
|
+ )
|
|
2779
|
+ gapi = GroupApi(
|
|
2780
|
+ current_user=admin,
|
|
2781
|
+ session=dbsession,
|
|
2782
|
+ config=self.app_config,
|
|
2783
|
+ )
|
|
2784
|
+ groups = [gapi.get_one_with_name('users')]
|
|
2785
|
+ test_user = uapi.create_user(
|
|
2786
|
+ email='test@test.test',
|
|
2787
|
+ password='pass',
|
|
2788
|
+ name='bob',
|
|
2789
|
+ groups=groups,
|
|
2790
|
+ timezone='Europe/Paris',
|
|
2791
|
+ do_save=True,
|
|
2792
|
+ do_notify=False,
|
|
2793
|
+ )
|
|
2794
|
+ uapi.save(test_user)
|
|
2795
|
+ transaction.commit()
|
|
2796
|
+ self.testapp.authorization = (
|
|
2797
|
+ 'Basic',
|
|
2798
|
+ (
|
|
2799
|
+ 'test@test.test',
|
|
2800
|
+ 'pass',
|
|
2801
|
+ )
|
|
2802
|
+ )
|
|
2803
|
+ params = {
|
|
2804
|
+ 'email': 'test2@test2.test2',
|
|
2805
|
+ 'password': 'mysuperpassword',
|
|
2806
|
+ 'profile': 'users',
|
|
2807
|
+ 'timezone': 'Europe/Paris',
|
|
2808
|
+ 'public_name': 'test user',
|
|
2809
|
+ 'email_notification': False,
|
|
2810
|
+ }
|
|
2811
|
+ res = self.testapp.post_json(
|
|
2812
|
+ '/api/v2/users',
|
|
2813
|
+ status=403,
|
|
2814
|
+ params=params,
|
|
2815
|
+ )
|
|
2816
|
+
|
|
2817
|
+
|
|
2818
|
+class TestUserWithNotificationEndpoint(FunctionalTest):
|
|
2819
|
+ """
|
|
2820
|
+ Tests for POST /api/v2/users/{user_id}
|
|
2821
|
+ """
|
|
2822
|
+ config_section = 'functional_test_with_mail_test_sync'
|
|
2823
|
+
|
|
2824
|
+ def test_api__create_user__ok_200__full_admin_with_notif(self):
|
|
2825
|
+ requests.delete('http://127.0.0.1:8025/api/v1/messages')
|
|
2826
|
+ self.testapp.authorization = (
|
|
2827
|
+ 'Basic',
|
|
2828
|
+ (
|
|
2829
|
+ 'admin@admin.admin',
|
|
2830
|
+ 'admin@admin.admin'
|
|
2831
|
+ )
|
|
2832
|
+ )
|
|
2833
|
+ params = {
|
|
2834
|
+ 'email': 'test@test.test',
|
|
2835
|
+ 'password': 'mysuperpassword',
|
|
2836
|
+ 'profile': 'users',
|
|
2837
|
+ 'timezone': 'Europe/Paris',
|
|
2838
|
+ 'public_name': 'test user',
|
|
2839
|
+ 'email_notification': True,
|
|
2840
|
+ }
|
|
2841
|
+ res = self.testapp.post_json(
|
|
2842
|
+ '/api/v2/users',
|
|
2843
|
+ status=200,
|
|
2844
|
+ params=params,
|
|
2845
|
+ )
|
|
2846
|
+ res = res.json_body
|
|
2847
|
+ assert res['user_id']
|
|
2848
|
+ user_id = res['user_id']
|
|
2849
|
+ assert res['created']
|
|
2850
|
+ assert res['is_active'] is True
|
|
2851
|
+ assert res['profile'] == 'users'
|
|
2852
|
+ assert res['email'] == 'test@test.test'
|
|
2853
|
+ assert res['public_name'] == 'test user'
|
|
2854
|
+ assert res['timezone'] == 'Europe/Paris'
|
|
2855
|
+
|
|
2856
|
+ dbsession = get_tm_session(self.session_factory, transaction.manager)
|
|
2857
|
+ admin = dbsession.query(models.User) \
|
|
2858
|
+ .filter(models.User.email == 'admin@admin.admin') \
|
|
2859
|
+ .one()
|
|
2860
|
+ uapi = UserApi(
|
|
2861
|
+ current_user=admin,
|
|
2862
|
+ session=dbsession,
|
|
2863
|
+ config=self.app_config,
|
|
2864
|
+ )
|
|
2865
|
+ user = uapi.get_one(user_id)
|
|
2866
|
+ assert user.email == 'test@test.test'
|
|
2867
|
+ assert user.validate_password('mysuperpassword')
|
|
2868
|
+
|
|
2869
|
+ # check mail received
|
|
2870
|
+ response = requests.get('http://127.0.0.1:8025/api/v1/messages')
|
|
2871
|
+ response = response.json()
|
|
2872
|
+ assert len(response) == 1
|
|
2873
|
+ headers = response[0]['Content']['Headers']
|
|
2874
|
+ assert headers['From'][0] == 'Tracim Notifications <test_user_from+0@localhost>' # nopep8
|
|
2875
|
+ assert headers['To'][0] == 'test user <test@test.test>'
|
|
2876
|
+ assert headers['Subject'][0] == '[TRACIM] Created account'
|
|
2877
|
+
|
|
2878
|
+ # TODO - G.M - 2018-08-02 - Place cleanup outside of the test
|
|
2879
|
+ requests.delete('http://127.0.0.1:8025/api/v1/messages')
|
|
2880
|
+
|
|
2881
|
+ def test_api__create_user__ok_200__limited_admin_with_notif(self):
|
|
2882
|
+ requests.delete('http://127.0.0.1:8025/api/v1/messages')
|
|
2883
|
+ self.testapp.authorization = (
|
|
2884
|
+ 'Basic',
|
|
2885
|
+ (
|
|
2886
|
+ 'admin@admin.admin',
|
|
2887
|
+ 'admin@admin.admin'
|
|
2888
|
+ )
|
|
2889
|
+ )
|
|
2890
|
+ params = {
|
|
2891
|
+ 'email': 'test@test.test',
|
|
2892
|
+ 'email_notification': True,
|
|
2893
|
+ }
|
|
2894
|
+ res = self.testapp.post_json(
|
|
2895
|
+ '/api/v2/users',
|
|
2896
|
+ status=200,
|
|
2897
|
+ params=params,
|
|
2898
|
+ )
|
|
2899
|
+ res = res.json_body
|
|
2900
|
+ assert res['user_id']
|
|
2901
|
+ user_id = res['user_id']
|
|
2902
|
+ assert res['created']
|
|
2903
|
+ assert res['is_active'] is True
|
|
2904
|
+ assert res['profile'] == 'users'
|
|
2905
|
+ assert res['email'] == 'test@test.test'
|
|
2906
|
+ assert res['public_name'] == 'test'
|
|
2907
|
+ assert res['timezone'] == ''
|
|
2908
|
+
|
|
2909
|
+ dbsession = get_tm_session(self.session_factory, transaction.manager)
|
|
2910
|
+ admin = dbsession.query(models.User) \
|
|
2911
|
+ .filter(models.User.email == 'admin@admin.admin') \
|
|
2912
|
+ .one()
|
|
2913
|
+ uapi = UserApi(
|
|
2914
|
+ current_user=admin,
|
|
2915
|
+ session=dbsession,
|
|
2916
|
+ config=self.app_config,
|
|
2917
|
+ )
|
|
2918
|
+ user = uapi.get_one(user_id)
|
|
2919
|
+ assert user.email == 'test@test.test'
|
|
2920
|
+ assert user.password
|
|
2921
|
+
|
|
2922
|
+ # check mail received
|
|
2923
|
+ response = requests.get('http://127.0.0.1:8025/api/v1/messages')
|
|
2924
|
+ response = response.json()
|
|
2925
|
+ assert len(response) == 1
|
|
2926
|
+ headers = response[0]['Content']['Headers']
|
|
2927
|
+ assert headers['From'][0] == 'Tracim Notifications <test_user_from+0@localhost>' # nopep8
|
|
2928
|
+ assert headers['To'][0] == 'test <test@test.test>'
|
|
2929
|
+ assert headers['Subject'][0] == '[TRACIM] Created account'
|
|
2930
|
+
|
|
2931
|
+ # TODO - G.M - 2018-08-02 - Place cleanup outside of the test
|
|
2932
|
+ requests.delete('http://127.0.0.1:8025/api/v1/messages')
|
|
2933
|
+
|
2637
|
2934
|
|
2638
|
2935
|
class TestUsersEndpoint(FunctionalTest):
|
2639
|
2936
|
# -*- coding: utf-8 -*-
|
2640
|
2937
|
"""
|
2641
|
|
- Tests for GET /api/v2/users/{user_id}
|
|
2938
|
+ Tests for GET /api/v2/users
|
2642
|
2939
|
"""
|
2643
|
2940
|
fixtures = [BaseFixture]
|
2644
|
2941
|
|
|
@@ -3088,6 +3385,68 @@ class TestSetEmailEndpoint(FunctionalTest):
|
3088
|
3385
|
res = res.json_body
|
3089
|
3386
|
assert res['email'] == 'mysuperemail@email.fr'
|
3090
|
3387
|
|
|
3388
|
+ def test_api__set_user_email__err_400__admin_same_email(self):
|
|
3389
|
+ dbsession = get_tm_session(self.session_factory, transaction.manager)
|
|
3390
|
+ admin = dbsession.query(models.User) \
|
|
3391
|
+ .filter(models.User.email == 'admin@admin.admin') \
|
|
3392
|
+ .one()
|
|
3393
|
+ uapi = UserApi(
|
|
3394
|
+ current_user=admin,
|
|
3395
|
+ session=dbsession,
|
|
3396
|
+ config=self.app_config,
|
|
3397
|
+ )
|
|
3398
|
+ gapi = GroupApi(
|
|
3399
|
+ current_user=admin,
|
|
3400
|
+ session=dbsession,
|
|
3401
|
+ config=self.app_config,
|
|
3402
|
+ )
|
|
3403
|
+ groups = [gapi.get_one_with_name('users')]
|
|
3404
|
+ test_user = uapi.create_user(
|
|
3405
|
+ email='test@test.test',
|
|
3406
|
+ password='pass',
|
|
3407
|
+ name='bob',
|
|
3408
|
+ groups=groups,
|
|
3409
|
+ timezone='Europe/Paris',
|
|
3410
|
+ do_save=True,
|
|
3411
|
+ do_notify=False,
|
|
3412
|
+ )
|
|
3413
|
+ uapi.save(test_user)
|
|
3414
|
+ transaction.commit()
|
|
3415
|
+ user_id = int(test_user.user_id)
|
|
3416
|
+
|
|
3417
|
+ self.testapp.authorization = (
|
|
3418
|
+ 'Basic',
|
|
3419
|
+ (
|
|
3420
|
+ 'admin@admin.admin',
|
|
3421
|
+ 'admin@admin.admin'
|
|
3422
|
+ )
|
|
3423
|
+ )
|
|
3424
|
+ # check before
|
|
3425
|
+ res = self.testapp.get(
|
|
3426
|
+ '/api/v2/users/{}'.format(user_id),
|
|
3427
|
+ status=200
|
|
3428
|
+ )
|
|
3429
|
+ res = res.json_body
|
|
3430
|
+ assert res['email'] == 'test@test.test'
|
|
3431
|
+
|
|
3432
|
+ # Set password
|
|
3433
|
+ params = {
|
|
3434
|
+ 'email': 'admin@admin.admin',
|
|
3435
|
+ 'loggedin_user_password': 'admin@admin.admin',
|
|
3436
|
+ }
|
|
3437
|
+ self.testapp.put_json(
|
|
3438
|
+ '/api/v2/users/{}/email'.format(user_id),
|
|
3439
|
+ params=params,
|
|
3440
|
+ status=400,
|
|
3441
|
+ )
|
|
3442
|
+ # Check After
|
|
3443
|
+ res = self.testapp.get(
|
|
3444
|
+ '/api/v2/users/{}'.format(user_id),
|
|
3445
|
+ status=200
|
|
3446
|
+ )
|
|
3447
|
+ res = res.json_body
|
|
3448
|
+ assert res['email'] == 'test@test.test'
|
|
3449
|
+
|
3091
|
3450
|
def test_api__set_user_email__err_403__admin_wrong_password(self):
|
3092
|
3451
|
dbsession = get_tm_session(self.session_factory, transaction.manager)
|
3093
|
3452
|
admin = dbsession.query(models.User) \
|