test_transfer_mail.py 3.63 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
30
        args,
        yaml.load(open(config), Loader=yaml.BaseLoader)
    )
    mover.transfer_mail()
    mover.cleanup()


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


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


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


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


def test_with_new_custom_dir():
    args = {
        'current_owner': 'testuser',
        'new_owner': 'testuser',
        'labels': ['label1', 'label2'],
97
        'label_prefix': None,
98
99
        'email_directory': '/var/tmp/gtm1a2b3c4d'
    }
Paul Arthur's avatar
Paul Arthur committed
100
    mover = transfer_mail.Mover(
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
        args,
        yaml.load(open(config), Loader=yaml.BaseLoader)
    )
    mover.transfer_mail()
    if not os.path.isdir('/var/tmp/gtm1a2b3c4d'):
        assert False
    mover.cleanup()
    if os.path.isdir('/var/tmp/gtm1a2b3c4d'):
        assert False


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


def test_transfer_with_bad_new_owner():
    with pytest.raises(SystemExit):
        args = {
            'current_owner': 'testuser',
            'new_owner': 'notauser',
133
134
            'labels': ['label1', 'label2'],
            'label_prefix': None
135
        }
Paul Arthur's avatar
Paul Arthur committed
136
        mover = transfer_mail.Mover(
137
138
139
140
141
            args,
            yaml.load(open(config), Loader=yaml.BaseLoader)
        )
        mover.transfer_mail()
        mover.cleanup()