|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
""" |
|
4
|
|
|
Created on Mon Jan 21 14:30:04 2019 |
|
5
|
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
|
7
|
|
|
""" |
|
8
|
|
|
|
|
9
|
|
|
import io |
|
10
|
|
|
import json |
|
11
|
|
|
|
|
12
|
|
|
from pyUSIrest.auth import Auth |
|
13
|
|
|
|
|
14
|
|
|
from unittest.mock import patch |
|
15
|
|
|
|
|
16
|
|
|
from django.core.management import call_command |
|
17
|
|
|
from django.test import TestCase |
|
18
|
|
|
|
|
19
|
|
|
from ..models import ManagedTeam |
|
20
|
|
|
|
|
21
|
|
|
from .common import BaseMixin, generate_token |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
class CommandsTestCase(BaseMixin, TestCase): |
|
25
|
|
|
@classmethod |
|
26
|
|
|
def setUpClass(cls): |
|
27
|
|
|
# calling my base class setup |
|
28
|
|
|
super().setUpClass() |
|
29
|
|
|
|
|
30
|
|
|
# starting mocked objects |
|
31
|
|
|
cls.mock_auth_patcher = patch('biosample.helpers.get_manager_auth') |
|
32
|
|
|
cls.mock_auth = cls.mock_auth_patcher.start() |
|
33
|
|
|
|
|
34
|
|
|
@classmethod |
|
35
|
|
|
def tearDownClass(cls): |
|
36
|
|
|
cls.mock_auth_patcher.stop() |
|
37
|
|
|
|
|
38
|
|
|
# calling base method |
|
39
|
|
|
super().tearDownClass() |
|
40
|
|
|
|
|
41
|
|
|
@patch('biosample.helpers.get_manager_auth') |
|
42
|
|
|
def test_fill_managed(self, my_auth): |
|
43
|
|
|
"""test fill_managed command""" |
|
44
|
|
|
|
|
45
|
|
|
# remove a managed team |
|
46
|
|
|
managed = ManagedTeam.objects.get(pk=2) |
|
47
|
|
|
managed.delete() |
|
48
|
|
|
|
|
49
|
|
|
# patch get_manager_auth value |
|
50
|
|
|
my_auth.return_value = Auth( |
|
51
|
|
|
token=generate_token( |
|
52
|
|
|
domains=[ |
|
53
|
|
|
'subs.test-team-1', |
|
54
|
|
|
'subs.test-team-2', |
|
55
|
|
|
'subs.test-team-3'] |
|
56
|
|
|
) |
|
57
|
|
|
) |
|
58
|
|
|
|
|
59
|
|
|
# calling commands |
|
60
|
|
|
call_command('fill_managed') |
|
61
|
|
|
|
|
62
|
|
|
# get all managedteams object |
|
63
|
|
|
self.assertEqual(ManagedTeam.objects.count(), 3) |
|
64
|
|
|
self.assertTrue(my_auth.called) |
|
65
|
|
|
|
|
66
|
|
|
def test_get_json_for_biosample(self): |
|
67
|
|
|
|
|
68
|
|
|
args = ["--submission", 1] |
|
69
|
|
|
|
|
70
|
|
|
# https://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python |
|
71
|
|
|
with patch('sys.stdout', new_callable=io.StringIO) as handle: |
|
72
|
|
|
call_command('get_json_for_biosample', *args) |
|
73
|
|
|
handle.seek(0) |
|
74
|
|
|
|
|
75
|
|
|
data = json.load(handle) |
|
76
|
|
|
self.assertIsInstance(data, dict) |
|
77
|
|
|
|