test_searching.py 5.22 KB
Newer Older
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
'''
Sample json for the advance search

    { "searchText" : "somegroup" }
or
    { "owner" : "uid=uniqname,ou=people,dc=umich,dc=umich"}
or
    { "cn" : "somegroup" }
or
    { "cn" : "somegroup", "cnSearchType":"contains" }
When searching by common name, cn, search types are

starts with ( default )
matches exactly
contains
ends with
'''

import unittest
import logging
import requests
import json
import urllib.parse

from apidirectory import ApiDirectory

logger = logging.getLogger(__name__)

class IamGroupSearchTests(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"


    # Search for a group that contains ITS somewhere in the name.
    def test_search(self):
        api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
        url_endpoint = 'search'
        url = IamGroupSearchTests.url_base + '/' + url_endpoint

        data = {'cn': 'ITS', "cnSearchType":"contains" }

        response = requests.post(
            url=url,
            data=json.dumps(data),
            headers=api.build_headers(),
            timeout=10
        )
        print(api.build_headers())
        print('Response: {}'.format(response))
        print('JSON: {}'.format(response.json()))

        self.assertEqual(response.status_code, requests.codes.ok)


    # Search for a group ITS.  Same behavior if you type a name in the quick search box.
    def test_simple_search(self):
        api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
        url_endpoint = 'search'
        url = IamGroupSearchTests.url_base + '/' + url_endpoint

        data = {'searchText': 'ITS'}

        response = requests.post(
            url=url,
            data=json.dumps(data),
            headers=api.build_headers(),
            timeout=10
        )
        print(api.build_headers())
        print('Response: {}'.format(response))
        print('JSON: {}'.format(response.json()))

        self.assertEqual(response.status_code, requests.codes.ok)


    # Search for a member in the groups Im an owner.
    def test_search_mygroups_for_member(self):
        api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
        url_endpoint = 'searchForItem'
        name = "uid=jbouffor,ou=people,dc=umich,dc=edu"
        # name = "cn=group name,ou=user groups,ou=groups,dc=umich,dc=edu"
        # name = "someone@yahoo.com"
        encoded = urllib.parse.quote(name)
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
        url = IamGroupSearchTests.url_base + '/' + url_endpoint + '/' + encoded

        response = requests.get(
            url=url,
            headers=api.build_headers(),
            timeout=10
        )
        print(api.build_headers())
        print('Response: {}'.format(response))
        print('JSON: {}'.format(response.json()))

        self.assertEqual(response.status_code, requests.codes.ok)

        # Search for a member in the groups Im an owner.

    def test_find_person_by_uniqname(self):
        api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
        url_endpoint = 'find/person'
        uniqname = "jbouffor"
        #Since uniqname are alpha numeric without spaces this is not really needed.
        encoded = urllib.parse.quote(uniqname)
        url = IamGroupSearchTests.url_base + '/' + url_endpoint + '/' + encoded

        response = requests.get(
            url=url,
            headers=api.build_headers(),
            timeout=10
        )
        print(api.build_headers())
        print('Response: {}'.format(response))
        print('JSON: {}'.format(response.json()))

        self.assertEqual(response.status_code, requests.codes.ok)

    def test_find_group_by_name_or_person(self):
        api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
        url_endpoint = 'find/both'
        uniqname = "post-its-notes"
        #Since uniqname are alpha numeric without spaces this is not really needed.
        encoded = urllib.parse.quote(uniqname)
        url = IamGroupSearchTests.url_base + '/' + url_endpoint + '/' + encoded

        response = requests.get(
            url=url,
            headers=api.build_headers(),
            timeout=10
        )
        print(api.build_headers())
        print('Response: {}'.format(response))
        print('JSON: {}'.format(response.json()))

        self.assertEqual(response.status_code, requests.codes.ok)

    def test_find_group_by_name(self):
        api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
        url_endpoint = 'find/typeahead'
        uniqname = "post-its-notes"
        # Since uniqname are alpha numeric without spaces this is not really needed.
        encoded = urllib.parse.quote(uniqname)
        url = IamGroupSearchTests.url_base + '/' + url_endpoint + '/' + encoded
148
149
150
151
152
153
154
155
156
157
158

        response = requests.get(
            url=url,
            headers=api.build_headers(),
            timeout=10
        )
        print(api.build_headers())
        print('Response: {}'.format(response))
        print('JSON: {}'.format(response.json()))

        self.assertEqual(response.status_code, requests.codes.ok)