forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_node_license.py
157 lines (131 loc) · 5.34 KB
/
test_node_license.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import json
from unittest import mock
import pytest
import builtins
from django.db.utils import IntegrityError
from osf.models.licenses import serialize_node_license_record, serialize_node_license
from osf.utils.migrations import ensure_licenses
from osf.exceptions import NodeStateError
from framework.auth.core import Auth
from osf.models import (
NodeLicense,
NodeLog
)
from osf_tests.factories import (
AuthUserFactory,
ProjectFactory,
NodeLicenseRecordFactory
)
CHANGED_NAME = 'FOO BAR'
CHANGED_TEXT = 'Some good new text'
CHANGED_PROPERTIES = ['foo', 'bar']
LICENSE_TEXT = json.dumps({
'MIT': {
'name': CHANGED_NAME,
'text': CHANGED_TEXT,
'properties': CHANGED_PROPERTIES
}
})
@pytest.mark.django_db
class TestNodeLicenses:
@pytest.fixture()
def user(self):
return AuthUserFactory()
@pytest.fixture()
def node(self, node_license, user):
node = ProjectFactory(creator=user)
node.node_license = NodeLicenseRecordFactory(
node_license=node_license,
year=self.YEAR,
copyright_holders=self.COPYRIGHT_HOLDERS
)
node.save()
return node
LICENSE_NAME = 'MIT License'
YEAR = '2105'
COPYRIGHT_HOLDERS = ['Foo', 'Bar']
@pytest.fixture()
def node_license(self):
return NodeLicense.objects.get(name=self.LICENSE_NAME)
def test_serialize_node_license(self, node_license):
serialized = serialize_node_license(node_license)
assert serialized['name'] == self.LICENSE_NAME
assert serialized['id'] == node_license.license_id
assert serialized['text'] == node_license.text
def test_serialize_node_license_record(self, node, node_license):
serialized = serialize_node_license_record(node.node_license)
assert serialized['name'] == self.LICENSE_NAME
assert serialized['id'] == node_license.license_id
assert serialized['text'] == node_license.text
assert serialized['year'] == self.YEAR
assert serialized['copyright_holders'] == self.COPYRIGHT_HOLDERS
def test_serialize_node_license_record_None(self, node):
node.node_license = None
serialized = serialize_node_license_record(node.node_license)
assert serialized == {}
def test_copy_node_license_record(self, node):
record = node.node_license
copied = record.copy()
assert copied._id is not None
assert record._id != copied._id
for prop in ('license_id', 'name', 'node_license'):
assert getattr(record, prop) == getattr(copied, prop)
def test_license_uniqueness_on_id_is_enforced_in_the_database(self):
NodeLicense(license_id='foo', name='bar', text='baz').save()
with pytest.raises(IntegrityError):
NodeLicense(license_id='foo', name='buz', text='boo').save()
def test_ensure_licenses_updates_existing_licenses(self):
assert ensure_licenses() == (0, 18)
def test_ensure_licenses_no_licenses(self):
before_count = NodeLicense.objects.all().count()
NodeLicense.objects.all().delete()
assert not NodeLicense.objects.all().count()
ensure_licenses()
assert before_count == NodeLicense.objects.all().count()
def test_ensure_licenses_some_missing(self):
NodeLicense.objects.get(license_id='LGPL3').delete()
with pytest.raises(NodeLicense.DoesNotExist):
NodeLicense.objects.get(license_id='LGPL3')
ensure_licenses()
found = NodeLicense.objects.get(license_id='LGPL3')
assert found is not None
def test_ensure_licenses_updates_existing(self):
with mock.patch.object(builtins, 'open', mock.mock_open(read_data=LICENSE_TEXT)):
ensure_licenses()
MIT = NodeLicense.objects.get(license_id='MIT')
assert MIT.name == CHANGED_NAME
assert MIT.text == CHANGED_TEXT
assert MIT.properties == CHANGED_PROPERTIES
def test_node_set_node_license(self, node, user):
GPL3 = NodeLicense.objects.get(license_id='GPL3')
NEW_YEAR = '2014'
COPYLEFT_HOLDERS = ['Richard Stallman']
node.set_node_license(
{
'id': GPL3.license_id,
'year': NEW_YEAR,
'copyrightHolders': COPYLEFT_HOLDERS
},
auth=Auth(user),
save=True
)
assert node.node_license.license_id == GPL3.license_id
assert node.node_license.name == GPL3.name
assert node.node_license.copyright_holders == COPYLEFT_HOLDERS
assert node.logs.latest().action == NodeLog.CHANGED_LICENSE
def test_node_set_node_license_invalid(self, node, user):
with pytest.raises(NodeStateError):
node.set_node_license(
{
'id': 'SOME ID',
'year': 'foo',
'copyrightHolders': []
},
auth=Auth(user)
)
action = node.logs.latest().action if node.logs.count() else None
assert action != NodeLog.CHANGED_LICENSE
def test_manager_methods(self):
# Projects can't have CCBYNCND but preprints can
assert 'CCBYNCND' not in list(NodeLicense.objects.project_licenses().values_list('license_id', flat=True))
assert 'CCBYNCND' in list(NodeLicense.objects.preprint_licenses().values_list('license_id', flat=True))