test_transfer_mail.py 3.38 KB
Newer Older
Paul Arthur's avatar
Paul Arthur committed
1
from collab_admin_kit import transfer_mail
2
3
4
5
6
7
8
9
10
11
12
13
14
import os
import pytest
import yaml

data_dir = os.path.join(os.path.dirname(__file__) + '/data/')
config = data_dir + 'test_cak_config.yml'


def test_transfer_label_list():
    args = {
        'current_owner': 'testuser',
        'new_owner': 'testuser',
        'labels': ['label1', 'label2'],
15
        'label_prefix': None,
Rob Carleski's avatar
Rob Carleski committed
16
        'email_directory': None
17
    }
Paul Arthur's avatar
Paul Arthur committed
18
    mover = transfer_mail.Mover(
19
20
21
22
23
24
25
26
27
28
29
        args,
        yaml.load(open(config), Loader=yaml.BaseLoader)
    )
    mover.transfer_mail()


def test_transfer_label_file():
    args = {
        'current_owner': 'testuser',
        'new_owner': 'testuser',
        'label_file': data_dir + '/mock_gyb/test_transfer_mail.txt',
30
        'label_prefix': None,
Rob Carleski's avatar
Rob Carleski committed
31
        'email_directory': None
32
    }
Paul Arthur's avatar
Paul Arthur committed
33
    mover = transfer_mail.Mover(
34
35
36
37
38
39
40
41
42
43
44
        args,
        yaml.load(open(config), Loader=yaml.BaseLoader)
    )
    mover.transfer_mail()


def test_prefix_assignment():
    args = {
        'current_owner': 'testuser',
        'new_owner': 'testuser',
        'labels': ['label1', 'label2'],
Rob Carleski's avatar
Rob Carleski committed
45
46
        'label_prefix': 'prefix',
        'email_directory': None
47
    }
Paul Arthur's avatar
Paul Arthur committed
48
    mover = transfer_mail.Mover(
49
50
51
52
53
54
55
56
57
58
59
        args,
        yaml.load(open(config), Loader=yaml.BaseLoader)
    )
    assert mover.transfer['label1']['destination'] == 'prefix-label1'


def test_search_translation():
    args = {
        'current_owner': 'testuser',
        'new_owner': 'testuser',
        'label_file': data_dir + '/mock_gyb/test_transfer_mail.txt',
60
        'label_prefix': None,
Rob Carleski's avatar
Rob Carleski committed
61
        'email_directory': None
62
    }
Paul Arthur's avatar
Paul Arthur committed
63
    mover = transfer_mail.Mover(
64
65
66
67
68
69
70
71
72
73
74
75
76
        args,
        yaml.load(open(config), Loader=yaml.BaseLoader)
    )
    print(mover.transfer)
    expected = 'weir---l-bel-'
    assert mover.transfer['weir|) l^bel(']['dest_searchable'] == expected


def test_with_existing_custom_dir():
    args = {
        'current_owner': 'testuser',
        'new_owner': 'testuser',
        'labels': ['label1', 'label2'],
77
        'label_prefix': None,
78
79
        'email_directory': '/var/tmp'
    }
Paul Arthur's avatar
Paul Arthur committed
80
    mover = transfer_mail.Mover(
81
82
83
84
85
86
87
88
89
90
91
        args,
        yaml.load(open(config), Loader=yaml.BaseLoader)
    )
    mover.transfer_mail()


def test_with_new_custom_dir():
    args = {
        'current_owner': 'testuser',
        'new_owner': 'testuser',
        'labels': ['label1', 'label2'],
92
        'label_prefix': None,
93
94
        'email_directory': '/var/tmp/gtm1a2b3c4d'
    }
Paul Arthur's avatar
Paul Arthur committed
95
    mover = transfer_mail.Mover(
96
97
98
99
        args,
        yaml.load(open(config), Loader=yaml.BaseLoader)
    )
    mover.transfer_mail()
100
    assert os.path.isdir('/var/tmp/gtm1a2b3c4d')
101
102
103
104
105
106
107


def test_transfer_with_bad_current_owner():
    with pytest.raises(SystemExit):
        args = {
            'current_owner': 'notauser',
            'new_owner': 'testuser',
108
109
            'labels': ['label1', 'label2'],
            'label_prefix': None
110
        }
Paul Arthur's avatar
Paul Arthur committed
111
        mover = transfer_mail.Mover(
112
113
114
115
116
117
118
119
120
121
122
            args,
            yaml.load(open(config), Loader=yaml.BaseLoader)
        )
        mover.transfer_mail()


def test_transfer_with_bad_new_owner():
    with pytest.raises(SystemExit):
        args = {
            'current_owner': 'testuser',
            'new_owner': 'notauser',
123
124
            'labels': ['label1', 'label2'],
            'label_prefix': None
125
        }
Paul Arthur's avatar
Paul Arthur committed
126
        mover = transfer_mail.Mover(
127
128
129
130
            args,
            yaml.load(open(config), Loader=yaml.BaseLoader)
        )
        mover.transfer_mail()