1
|
|
|
import pytest |
2
|
|
|
import sys |
3
|
|
|
import groundwork |
4
|
|
|
from groundwork_validation.patterns import GwFileValidatorsPattern |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
def test_file_validator_init(): |
8
|
|
|
""" |
9
|
|
|
.. test:: GwFileValidator Init test |
10
|
|
|
:tags: gwfilevalidators |
11
|
|
|
|
12
|
|
|
""" |
13
|
|
|
class My_Plugin(GwFileValidatorsPattern): |
14
|
|
|
def __init__(self, app, **kwargs): |
15
|
|
|
self.name = "My_Plugin" |
16
|
|
|
super(My_Plugin, self).__init__(app, **kwargs) |
17
|
|
|
|
18
|
|
|
def activate(self): |
19
|
|
|
pass |
20
|
|
|
|
21
|
|
|
def deactivate(self): |
22
|
|
|
pass |
23
|
|
|
|
24
|
|
|
app = groundwork.App() |
25
|
|
|
plugin = My_Plugin(app) |
26
|
|
|
plugin.activate() |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def test_file_validator_hash(tmpdir): |
30
|
|
|
""" |
31
|
|
|
.. test:: GwFileValidator hash test |
32
|
|
|
:tags: gwfilevalidators |
33
|
|
|
:links: S_31C31; S_CFBC1 |
34
|
|
|
|
35
|
|
|
""" |
36
|
|
|
test_file_1 = tmpdir.mkdir("sub_1").join("test_1.txt") |
37
|
|
|
test_file_1.write("content") |
38
|
|
|
|
39
|
|
|
test_file_2 = tmpdir.mkdir("sub_2").join("test_2.txt") |
40
|
|
|
test_file_2.write("content") |
41
|
|
|
|
42
|
|
|
test_file_3 = tmpdir.mkdir("sub_3").join("test_2.txt") |
43
|
|
|
test_file_3.write("content_3") |
44
|
|
|
|
45
|
|
|
class My_Plugin(GwFileValidatorsPattern): |
46
|
|
|
def __init__(self, app, **kwargs): |
47
|
|
|
self.name = "My_Plugin" |
48
|
|
|
super(My_Plugin, self).__init__(app, **kwargs) |
49
|
|
|
|
50
|
|
|
def activate(self): |
51
|
|
|
pass |
52
|
|
|
|
53
|
|
|
def deactivate(self): |
54
|
|
|
pass |
55
|
|
|
|
56
|
|
|
app = groundwork.App() |
57
|
|
|
plugin = My_Plugin(app) |
58
|
|
|
plugin.activate() |
59
|
|
|
|
60
|
|
|
hash_1 = plugin.validators.file.hash(test_file_1.strpath) |
61
|
|
|
assert hash_1 is not None |
62
|
|
|
|
63
|
|
|
hash_2 = plugin.validators.file.hash(test_file_2.strpath) |
64
|
|
|
assert hash_2 is not None |
65
|
|
|
assert hash_1 == hash_2 |
66
|
|
|
|
67
|
|
|
hash_3 = plugin.validators.file.hash(test_file_3.strpath) |
68
|
|
|
assert hash_3 is not None |
69
|
|
|
assert hash_3 != hash_1 |
70
|
|
|
|
71
|
|
|
hash_object_1 = plugin.validators.file.hash(test_file_1.strpath, return_hash_object=True) |
72
|
|
|
assert hash_object_1 is not None |
73
|
|
|
|
74
|
|
|
hash_1_file = tmpdir.join("sub_1", "test_1.hash").strpath |
75
|
|
|
hash_1 = plugin.validators.file.hash(test_file_1.strpath, hash_file=hash_1_file) |
76
|
|
|
with open(hash_1_file, "r") as hash_1_fobject: |
77
|
|
|
assert hash_1_fobject.readline() == hash_1 |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
def test_file_validator_validate(tmpdir): |
81
|
|
|
""" |
82
|
|
|
.. test:: GwFileValidator Validate test |
83
|
|
|
:tags: gwfilevalidators |
84
|
|
|
:links: S_31C31; S_CFBC1 |
85
|
|
|
""" |
86
|
|
|
test_file_1 = tmpdir.mkdir("sub_1").join("test_1.txt") |
87
|
|
|
test_file_1.write("content") |
88
|
|
|
|
89
|
|
|
class My_Plugin(GwFileValidatorsPattern): |
90
|
|
|
def __init__(self, app, **kwargs): |
91
|
|
|
self.name = "My_Plugin" |
92
|
|
|
super(My_Plugin, self).__init__(app, **kwargs) |
93
|
|
|
|
94
|
|
|
def activate(self): |
95
|
|
|
pass |
96
|
|
|
|
97
|
|
|
def deactivate(self): |
98
|
|
|
pass |
99
|
|
|
|
100
|
|
|
app = groundwork.App() |
101
|
|
|
plugin = My_Plugin(app) |
102
|
|
|
plugin.activate() |
103
|
|
|
|
104
|
|
|
test_file_1_hash = tmpdir.join("sub_1", "test_1.hash") |
105
|
|
|
test_1_hash = plugin.validators.file.hash(test_file_1.strpath) |
106
|
|
|
test_file_1_hash.write(test_1_hash) |
107
|
|
|
|
108
|
|
|
assert plugin.validators.file.validate(test_file_1.strpath, test_1_hash) is True |
109
|
|
|
assert plugin.validators.file.validate(test_file_1.strpath, "NoWay") is False |
110
|
|
|
|
111
|
|
|
assert plugin.validators.file.validate(test_file_1.strpath, hash_file=test_file_1_hash.strpath) is True |
112
|
|
|
assert plugin.validators.file.validate(test_file_1.strpath, |
113
|
|
|
hash_file=test_file_1_hash.strpath, blocksize=1024) is True |
114
|
|
|
|
115
|
|
|
test_file_1_no_hash = tmpdir.join("sub_1", "test_1.no_hash") |
116
|
|
|
test_file_1_no_hash.write("NoWay") |
117
|
|
|
assert plugin.validators.file.validate(test_file_1.strpath, hash_file=test_file_1_no_hash.strpath) is False |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
def test_file_validator_validate_errors(tmpdir): |
121
|
|
|
""" |
122
|
|
|
.. test:: GwFileValidator Error tests |
123
|
|
|
:tags: gwfilevalidators |
124
|
|
|
|
125
|
|
|
""" |
126
|
|
|
test_file_1 = tmpdir.mkdir("sub_1").join("test_1.txt") |
127
|
|
|
test_file_1.write("content") |
128
|
|
|
|
129
|
|
|
class My_Plugin(GwFileValidatorsPattern): |
130
|
|
|
def __init__(self, app, **kwargs): |
131
|
|
|
self.name = "My_Plugin" |
132
|
|
|
super(My_Plugin, self).__init__(app, **kwargs) |
133
|
|
|
|
134
|
|
|
def activate(self): |
135
|
|
|
pass |
136
|
|
|
|
137
|
|
|
def deactivate(self): |
138
|
|
|
pass |
139
|
|
|
|
140
|
|
|
app = groundwork.App() |
141
|
|
|
plugin = My_Plugin(app) |
142
|
|
|
plugin.activate() |
143
|
|
|
|
144
|
|
|
with pytest.raises(ValueError): |
145
|
|
|
plugin.validators.file.validate(test_file_1.strpath) |
146
|
|
|
|
147
|
|
|
with pytest.raises(ValueError): |
148
|
|
|
plugin.validators.file.validate(test_file_1.strpath, "No", "NoFilePath") |
149
|
|
|
|
150
|
|
|
if sys.version_info.major < 3: |
151
|
|
|
with pytest.raises(IOError): |
152
|
|
|
plugin.validators.file.validate(test_file_1.strpath, hash_file="NoFilePath") |
153
|
|
|
else: |
154
|
|
|
with pytest.raises(FileNotFoundError): |
155
|
|
|
plugin.validators.file.validate(test_file_1.strpath, hash_file="NoFilePath") |
156
|
|
|
|