|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
| 3 | +import os |
3 | 4 | import datetime |
4 | 5 | import httplib as http |
5 | 6 | import time |
|
21 | 22 | from osf_tests.factories import (AuthUserFactory, ProjectFactory, |
22 | 23 | RegistrationFactory) |
23 | 24 | from website import settings |
| 25 | +from website.util.paths import webpack_asset |
24 | 26 | from addons.base import views |
25 | 27 | from addons.github.exceptions import ApiError |
26 | 28 | from addons.github.models import GithubFolder, GithubFile, GithubFileNode |
|
30 | 32 | from osf.models.files import BaseFileNode, TrashedFileNode |
31 | 33 | from website.project import new_private_link |
32 | 34 | from website.project.views.node import _view_project as serialize_node |
| 35 | +from website.project.views.node import serialize_addons, collect_node_config_js |
33 | 36 | from website.util import api_url_for, rubeus |
34 | 37 | from dateutil.parser import parse as parse_date |
35 | 38 | from framework import sentry |
@@ -1099,3 +1102,56 @@ def test_other_addon_redirect_download(self): |
1099 | 1102 | provider='mycooladdon', |
1100 | 1103 | ) |
1101 | 1104 | assert_urls_equal(res.location, expected_url) |
| 1105 | + |
| 1106 | +class TestViewUtils(OsfTestCase): |
| 1107 | + |
| 1108 | + def setUp(self): |
| 1109 | + super(TestViewUtils, self).setUp() |
| 1110 | + self.user = AuthUserFactory() |
| 1111 | + self.auth_obj = Auth(user=self.user) |
| 1112 | + self.node = ProjectFactory(creator=self.user) |
| 1113 | + self.session = Session(data={'auth_user_id': self.user._id}) |
| 1114 | + self.session.save() |
| 1115 | + self.cookie = itsdangerous.Signer(settings.SECRET_KEY).sign(self.session._id) |
| 1116 | + self.configure_addon() |
| 1117 | + self.JWE_KEY = jwe.kdf(settings.WATERBUTLER_JWE_SECRET.encode('utf-8'), settings.WATERBUTLER_JWE_SALT.encode('utf-8')) |
| 1118 | + |
| 1119 | + def configure_addon(self): |
| 1120 | + self.user.add_addon('github') |
| 1121 | + self.user_addon = self.user.get_addon('github') |
| 1122 | + self.oauth_settings = GitHubAccountFactory(display_name='john') |
| 1123 | + self.oauth_settings.save() |
| 1124 | + self.user.external_accounts.add(self.oauth_settings) |
| 1125 | + self.user.save() |
| 1126 | + self.node.add_addon('github', self.auth_obj) |
| 1127 | + self.node_addon = self.node.get_addon('github') |
| 1128 | + self.node_addon.user = 'john' |
| 1129 | + self.node_addon.repo = 'youre-my-best-friend' |
| 1130 | + self.node_addon.user_settings = self.user_addon |
| 1131 | + self.node_addon.external_account = self.oauth_settings |
| 1132 | + self.node_addon.save() |
| 1133 | + self.user_addon.oauth_grants[self.node._id] = {self.oauth_settings._id: []} |
| 1134 | + self.user_addon.save() |
| 1135 | + |
| 1136 | + def test_serialize_addons(self): |
| 1137 | + addon_dicts = serialize_addons(self.node) |
| 1138 | + |
| 1139 | + enabled_addons = [addon for addon in addon_dicts if addon['enabled']] |
| 1140 | + assert len(enabled_addons) == 2 |
| 1141 | + assert enabled_addons[0]['short_name'] == 'github' |
| 1142 | + assert enabled_addons[1]['short_name'] == 'osfstorage' |
| 1143 | + |
| 1144 | + default_addons = [addon for addon in addon_dicts if addon['default']] |
| 1145 | + assert len(default_addons) == 1 |
| 1146 | + assert default_addons[0]['short_name'] == 'osfstorage' |
| 1147 | + |
| 1148 | + def test_collect_node_config_js(self): |
| 1149 | + |
| 1150 | + addon_dicts = serialize_addons(self.node) |
| 1151 | + |
| 1152 | + asset_paths = collect_node_config_js(addon_dicts) |
| 1153 | + |
| 1154 | + # Default addons should be in addon dicts, but they have no js assets because you can't |
| 1155 | + # connect/disconnect from them, think osfstorage, there's no node-cfg for that. |
| 1156 | + default_addons = [addon['short_name'] for addon in addon_dicts if addon['default']] |
| 1157 | + assert not any('/{}/'.format(addon) in asset_paths for addon in default_addons) |
0 commit comments