test_crud_operations.py 6.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
import unittest
import logging
import requests
import json
import urllib.parse

from apidirectory import ApiDirectory

logger = logging.getLogger(__name__)

John C Boufford's avatar
John C Boufford committed
11
12
13

# This test class test Create, Read, Update, and Delete (CRUD) oeprations.
# The first test has a one because when I run the test they run in alphbetic order
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# which may be different in your IDE, or command line.
class IamGroupCRUDTests(unittest.TestCase):

    client_id = "e06fb405-c58e-4334-b746-dd5969575bf2"
    secret = "G5oT3vL1uK8xS7rO5sJ4nH7bK5iB6hN5jN5dI0nL8tO5bG4tT5"
    scope = "iamgroups"
    token_url = "https://apigw-tst.it.umich.edu/um/inst/oauth2/token"
    url_base = "https://apigw-tst.it.umich.edu/um/iamGroups"

    # Test a create operation. Notice that the data structure needs to be converted to json.
    # The url_endpoint is obtain from the API directory
    # The api.build_headers builds the header needed for two factor oauth.
    def test_1_create(self):
        api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
        url_endpoint = 'create'
        url = IamGroupCRUDTests.url_base + '/' + url_endpoint
John C Boufford's avatar
John C Boufford committed
30
        data = {'name': 'iamGroupTestJB'}
31
32
33
34

        response = requests.post(
            url=url,
            data=json.dumps(data),
John C Boufford's avatar
John C Boufford committed
35
36
            headers=api.build_headers(),
            timeout=10
37
38
39
40
41
42
        )

        print('Response: {}'.format(response))
        print('JSON: {}'.format(response.json()))
        self.assertEqual(response.status_code, requests.codes.ok)

John C Boufford's avatar
John C Boufford committed
43
    # The Read operation takes a dn on the url.  Because a dn contains spaces, commas and equals it
44
45
46
47
    # needs to url encode. Note the test at the bottom of the unit test.
    def test_2_read(self):
        api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
        dn = "cn=iamGroupTestJB,ou=user groups,ou=groups,dc=umich,dc=edu"
John C Boufford's avatar
John C Boufford committed
48
        encoded = urllib.parse.quote(dn)
49
        url_endpoint = 'profile/dn'
John C Boufford's avatar
John C Boufford committed
50
        url = IamGroupCRUDTests.url_base + '/' + url_endpoint + "/" + encoded
51
52
53

        response = requests.get(
            url=url,
John C Boufford's avatar
John C Boufford committed
54
55
            headers=api.build_headers(),
            timeout=10
56
57
58
59
60
61
        )

        print('Response: {}'.format(response))
        print('JSON: {}'.format(response.json()))
        self.assertEqual(response.status_code, requests.codes.ok)

John C Boufford's avatar
John C Boufford committed
62
63
        # note that the returned json is a dictionary object with a key of group.
        # the value is a list of groups and each group is a dictionary.
64
65
66
67
68
69
70
71
72
73
        map = response.json()
        list = map['group']
        profile = list[0]
        self.assertEqual(profile['dn'], dn)

    # update. There are many update operations.  I am showing how to modifiy a moderator.
    def test_3_update(self):
        api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
        url_endpoint = 'update/moderator'
        url = IamGroupCRUDTests.url_base + '/' + url_endpoint
John C Boufford's avatar
John C Boufford committed
74
75
76
        data = {'dn': 'cn=iamGroupTestJB,ou=user groups,ou=groups,dc=umich,dc=edu',
                'moderator': [{'email': 'john.boufford@yahoo.com'},
                              {'name': 'John Boufford', 'email': 'jbouffor@yahoo.com'}]}
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

        response = requests.post(
            url=url,
            data=json.dumps(data),
            headers=api.build_headers(),
            timeout=10
        )

        print('Response: {}'.format(response))
        print('JSON: {}'.format(response.json()))
        self.assertEqual(response.status_code, requests.codes.ok)

    # delete an existing group.  Again the dn is part of the url so it should be encoded.
    def test_4_delete(self):
        api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
        dn = "cn=iamGroupTestJB,ou=user groups,ou=groups,dc=umich,dc=edu"
John C Boufford's avatar
John C Boufford committed
93
        encoded = urllib.parse.quote(dn)
94
        url_endpoint = 'delete'
John C Boufford's avatar
John C Boufford committed
95
        url = IamGroupCRUDTests.url_base + '/' + url_endpoint + "/" + encoded
96
97
98
99
100
101

        response = requests.get(
            url=url,
            headers=api.build_headers(),
            timeout=10
        )
John C Boufford's avatar
John C Boufford committed
102

103
104
105
106
107
108
109
110
111
112
113
114
115
        print('Response: {}'.format(response))
        print('JSON: {}'.format(response.json()))
        self.assertEqual(response.status_code, requests.codes.ok)

    # Note that I used urllib.parse.quote which converts spaces to %20.
    # the urllib.parse.quote_plus converts spaces to a plus sign.  Which fails in this api.
    def test_urlencoding(self):
        expected = "cn%3DiamGroupTestJB%2Cou%3Duser%20groups%2Cou%3Dgroups%2Cdc%3Dumich%2Cdc%3Dedu"
        pre = "cn=iamGroupTestJB,ou=user groups,ou=groups,dc=umich,dc=edu"
        post = urllib.parse.quote(pre)
        print(post)
        self.assertEqual(post, expected)

John C Boufford's avatar
John C Boufford committed
116
    # Test check if the group name is avaliable and follows all the rules.
117
118
119
120
    # Make sure to encode the name because the name can contain spaces and other specail chars.
    def test_isValid(self):
        api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
        name = "iamGroupTestJB"
John C Boufford's avatar
John C Boufford committed
121
        encoded = urllib.parse.quote(name)
122
        url_endpoint = 'mygroup/isValidName'
John C Boufford's avatar
John C Boufford committed
123
        url = IamGroupCRUDTests.url_base + '/' + url_endpoint + "/" + encoded
124
125
126
127
128
129

        response = requests.get(
            url=url,
            headers=api.build_headers(),
            timeout=10
        )
John C Boufford's avatar
John C Boufford committed
130

131
132
133
        print('Response: {}'.format(response))
        print('JSON: {}'.format(response.json()))
        self.assertEqual(response.status_code, requests.codes.ok)
John C Boufford's avatar
John C Boufford committed
134
        map = response.json()
135
136
137
138
139
140
141
        self.assertTrue(map['valid'])

    # Test check if the group name is avaliable and follows all the rules. fails
    # Make sure to encode the name because the name can contain spaces and other specail chars.
    def test_isValid_failed(self):
        api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
        name = "jbouffor"
John C Boufford's avatar
John C Boufford committed
142
        encoded = urllib.parse.quote(name)
143
        url_endpoint = 'mygroup/isValidName'
John C Boufford's avatar
John C Boufford committed
144
        url = IamGroupCRUDTests.url_base + '/' + url_endpoint + "/" + encoded
145
146
147
148
149
150

        response = requests.get(
            url=url,
            headers=api.build_headers(),
            timeout=10
        )
John C Boufford's avatar
John C Boufford committed
151

152
153
154
        print('Response: {}'.format(response))
        print('JSON: {}'.format(response.json()))
        self.assertEqual(response.status_code, requests.codes.ok)
John C Boufford's avatar
John C Boufford committed
155
        map = response.json()
156
        self.assertFalse(map['valid'])