d2l_sdk 0.1.12 → 0.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +0,0 @@
1
- require_relative 'd2l_api'
2
-
3
- def multithreaded_search_test
4
- ap multithreaded_user_search(test)
5
- end
6
- multithreaded_search_test
@@ -1,284 +0,0 @@
1
-
2
- require 'rubygems'
3
- require 'awesome_print' # awesome_print gem
4
- require 'base64'
5
- require 'json'
6
- require 'restclient' # rest-client gem
7
- require 'openssl'
8
- require 'open-uri'
9
- require 'launchy'
10
- require 'colorize'
11
-
12
- # vars
13
- $hostname = 'wiutest.desire2learn.com' # set hostname to the test server
14
- puts '[+] Host set to: '.yellow + $hostname
15
- # User ID = api-user
16
- # User PW = see keepass
17
- $user_id = 'rV5wdZwBbnT4prgaLlqx05'
18
- $user_key = 'xVee4KJhixBCQphf-3Wuf9'
19
-
20
- # #OG app ID and key
21
- $app_id = 'ysuBVIqmRDaJpnVEOb8Ylg'
22
- $app_key = 't3Rro3P91i_U1let5vd8Wg'
23
-
24
- # #Zero permissions account app
25
- # $app_id = 'KG3-PlRdIghGK_mc4M0QAg'
26
- # $app_key = '0VX8ZBNh5_Qip3SmNL57eA'
27
-
28
- def create_authenticated_uri(path, http_method)
29
- parsed_url = URI.parse(path.downcase)
30
- uri_scheme = 'https'
31
- query_string = get_query_string(parsed_url.path, http_method)
32
- uri = uri_scheme + '://' + $hostname + parsed_url.path + query_string
33
- uri << '&' + parsed_url.query if parsed_url.query
34
- uri
35
- end
36
-
37
- def build_authenticated_uri_query_string(signature, timestamp)
38
- "?x_a=#{$app_id}"\
39
- "&x_b=#{$user_id}"\
40
- "&x_c=#{get_base64_hash_string($app_key, signature)}"\
41
- "&x_d=#{get_base64_hash_string($user_key, signature)}"\
42
- "&x_t=#{timestamp}"
43
- end
44
-
45
- def authenticate_uri(path, http_method)
46
- get_query_string(path, http_method)
47
- end
48
-
49
- def format_signature(path, http_method, timestamp)
50
- http_method.upcase + '&' + path.encode('UTF-8') + '&' + timestamp.to_s
51
- end
52
-
53
- def get_base64_hash_string(key, signature)
54
- hash = OpenSSL::HMAC.digest('sha256', key, signature)
55
- Base64.urlsafe_encode64(hash).delete('=')
56
- end
57
-
58
- def get_query_string(path, http_method)
59
- timestamp = Time.now.to_i
60
- signature = format_signature(path, http_method, timestamp)
61
- build_authenticated_uri_query_string(signature, timestamp)
62
- end
63
-
64
- def _post(url, payload, headers)
65
- RestClient.post(url, payload.to_json, headers)
66
- end
67
- =begin
68
- { 'OrgDefinedId' => '12345678',
69
- 'FirstName' => 'test',
70
- 'MiddleName' => 'test1',
71
- 'LastName' => 'test12',
72
- 'ExternalEmail' => 'None',
73
- 'UserName' => 'test12345a',
74
- 'RoleId' => 105,
75
- 'IsActive' => false,
76
- 'SendCreationEmail' => false
77
- }
78
- =end
79
- # CREATE
80
- def create_sample_user_data
81
- http_method = 'POST'
82
- puts '[-] Testing ' + http_method + ' through create_sample_user_data'
83
- path = '/d2l/api/lp/1.4/users/'
84
- payload = { 'OrgDefinedId' => '', # String
85
- 'FirstName' => 'TestUser', # String
86
- 'MiddleName' => 'Test', # String
87
- 'LastName' => 'Test', # String
88
- 'ExternalEmail' => nil, # String (nil or well-formed email addr)
89
- 'UserName' => 'test12345a', # String
90
- 'RoleId' => 110, # number
91
- 'IsActive' => false, # bool
92
- 'SendCreationEmail' => false, # bool
93
- }
94
-
95
- ap payload
96
- puts '[-] Path used: ' + path
97
- headers = { content_type: :json }
98
- # POST /d2l/api/lp/1.4/users/
99
- test_uri = create_authenticated_uri(path, http_method)
100
- RestClient.post(test_uri, payload.to_json, headers)
101
- puts '[+] sample user data completed successfully'.green
102
- end
103
-
104
- def update_sample_user_data(_userId, _newData)
105
- http_method = 'PUT'
106
- puts '[-] Testing ' + http_method + ' through update_sample_user_data'
107
- path = '/d2l/api/lp/1.4/users/' + '47906' #'JtVGn4cUKz' # to_s
108
- payload = {
109
- 'OrgDefinedId' => '',
110
- 'FirstName' => 'API',
111
- 'MiddleName' => 'changedName',
112
- 'LastName' => 'User',
113
- 'ExternalEmail' => 'help@wiu.edu',
114
- 'UserName' => 'api-user',
115
- 'Activation' => {
116
- 'IsActive' => true
117
- }
118
- }
119
- print 'user JSON = '
120
- ap payload
121
- headers = { content_type: :json }
122
- # POST /d2l/api/lp/1.4/users/
123
- test_uri = create_authenticated_uri(path, http_method)
124
- puts '[-] Path used: ' + path
125
- RestClient.put(test_uri, payload.to_json, headers)
126
- puts '[+] sample user data updated successfully'.green
127
- # argument newData is used to update data of the sample users
128
- # will need to get the userId of the sample first
129
- end
130
-
131
- def get_query(uri_string)
132
- RestClient.get(uri_string) do |response, request, result, &block|
133
- begin
134
- case response.code
135
- when 200
136
- puts '[+] The request has succeeded.'.green
137
- print '[-] Class utilized: '
138
- puts JSON.parse(response).class
139
- # puts "\n[+] Unformatted JSON parsed response: "
140
- # puts JSON.parse(response)
141
- puts "\n[-] awesome_print Formatted JSON parsed response: "
142
- ap JSON.parse(response)
143
- else
144
- handle_response(response.code)
145
- response.return!(request, result, &block)
146
- puts '[!] Get query failed, see above response code'.red
147
- end
148
- rescue
149
- ap response.code
150
- end
151
- end
152
- end
153
-
154
- def handle_response(code)
155
- case code
156
- when 400
157
- puts '[!] 400: Bad Request'
158
- when 401
159
- puts '[!] 401: Unauthorized'
160
-
161
- when 403
162
- print '[!] Error Code Forbidden 403: accessing the page or resource '\
163
- 'you were trying to reach is absolutely forbidden for some reason.'
164
- when 404
165
- puts '[!] 404: Not Found'
166
- when 405
167
- puts '[!] 405: Method Not Allowed'
168
- when 406
169
- puts 'Unacceptable Type'\
170
- 'Unable to provide content type matching the client\'s Accept header.'
171
- when 412
172
- puts '[!] 412: Precondition failed\n'\
173
- 'Unsupported or invalid parameters, or missing required parameters.'
174
- when 415
175
- puts '[!] 415: Unsupported Media Type'\
176
- 'A PUT or POST payload cannot be accepted.'
177
- when 423
178
- raise SomeCustomExceptionIfYouWant
179
- when 500
180
- puts '[!] 500: General Service Error\n'\
181
- 'Empty response body. The service has encountered an unexpected'\
182
- 'state and cannot continue to handle your action request.'
183
- when 504
184
- puts '[!] 504: Service Error'
185
- end
186
- end
187
-
188
- # example paths
189
- # -----------------
190
- # path = '/d2l/api/lp/1.4/users/'
191
- # path = '/d2l/api/lp/1.4/enrollments/users/47892/orgUnits/'
192
- # path = '/d2l/api/versions/'
193
-
194
- # Test1
195
- def testRolesViewing
196
- puts '[-] Test1: Checking if Current User can check existing roles'.cyan
197
- path = '/d2l/api/lp/1.4/roles/'
198
- http_method = 'GET'
199
- test_uri = create_authenticated_uri(path, http_method)
200
- puts '[-] Authenticated URI Created For: ' + path
201
- # General Brightspace API response behaviors
202
- get_query(test_uri)
203
- puts "[+] Test1 Succeeded\n".green
204
- rescue
205
- puts '[!] Test1 failed'.red
206
- end
207
-
208
- # Test 2
209
- def testWhoAmI
210
- puts "[-] Test2: Getting Current User's WhoAmI".cyan
211
- path = '/d2l/api/lp/1.4/users/whoami'
212
- http_method = 'GET'
213
- test_uri = create_authenticated_uri(path, http_method)
214
- puts '[-] Authenticated URI Created: ' + path
215
- # General Brightspace API response behaviors
216
- get_query(test_uri)
217
- puts "[+] Test2 Succeeded\n".green
218
- rescue
219
- puts '[!] Test2 failed'.red
220
- end
221
-
222
- # TEST 3
223
- def testCreateUser
224
- puts '[-] Test3: testing create sample user data function'.cyan
225
- create_sample_user_data # ERROR 403 Forbidden (lack of access/back-end)
226
- puts "[+] Test3 Succeeded\n".green
227
- rescue => error
228
- ap error
229
- puts "[!] Test3 failed\n".red
230
- # ap error.backtrace
231
- end
232
-
233
- # Test 4
234
- def testUpdateUser
235
- puts '[-] Test4: testing update sample user data function'.cyan
236
- update_sample_user_data(1, 1) # TCP failed?
237
- puts "[+] Test4 Succeeded\n".green
238
- rescue => error
239
- ap error
240
- ap error.backtrace
241
- puts "[!] Test4 failed\n".red
242
- end
243
-
244
- # Test 5
245
- def testWhoAnotherUserIs(id)
246
- puts "[-] Test5: Getting Current User's Ability to view other user profiles".cyan
247
- path = '/d2l/api/lp/1.4/users/' + id # '/d2l/api/lp/1.4/users/whoami'
248
- http_method = 'GET'
249
- test_uri = create_authenticated_uri(path, http_method)
250
- puts '[-] Authenticated URI Created: ' + path
251
- # General Brightspace API response behaviors
252
- get_query(test_uri)
253
- puts "[+] Test5 Succeeded\n".green
254
- rescue
255
- puts '[!] Test5 failed'.red
256
- end
257
-
258
- def testSearchByUserName(username)
259
- puts "[-] Test6: Using brightspace search functionality using UserName".cyan
260
- path = '/d2l/api/lp/1.4/users/' + "?UserName=" + username
261
- http_method = 'GET'
262
- test_uri = create_authenticated_uri(path, http_method)
263
- puts '[-] Authenticated URI Created: ' + path
264
- # General Brightspace API response behaviors
265
- get_query(test_uri)
266
- puts "[+] Test6 Succeeded\n".green
267
- rescue
268
- puts '[!] Test6 failed'.red
269
- end
270
-
271
-
272
- #testRolesViewing
273
- #testWhoAmI
274
- #testCreateUser
275
- #testUpdateUser
276
- #testWhoAnotherUserIs('47906')
277
- testSearchByUserName("test12345a")
278
-
279
- # ap response.return!(request, result, &block)
280
- # hash = OpenSSL::HMAC.digest('sha256', "#{$user_id}&#{$user_key}", $app_key)
281
- # x_c = Base64.urlsafe_encode64(hash).delete('=')
282
- # puts '[+] Hash: ' + x_c
283
- # $timestamp = Time.now.to_i # current hour
284
- # $timestamp = 1406652368
@@ -1,36 +0,0 @@
1
- require 'csv'
2
- require 'Time'
3
- #Smaller version of Enroll/Withdrawals-01
4
- csv_fname = "lib/export_jobs/Enrollments and Withdrawals-01-17-2017T10-40-29.csv"
5
- # "export_jobs/Enrollments and Withdrawals-01-17-2017T14-11-41.csv"
6
- # "lib/export_jobs/Enrollments and Withdrawals-01-17-2017T10-40-29.csv"
7
-
8
- def fix_csv_format(csv_fname)
9
- # for this csv file...
10
- File.open(csv_fname + ".csv", 'w') do |file|
11
- # set row num to 0 to keep track of headers
12
- row_num = 0
13
- # for each row
14
- CSV.foreach(csv_fname) do |row|
15
- # the line is initialized as an empty string
16
- line = ""
17
- # for all of these values
18
- row[0..-1].each do |value|
19
- # If it a UTC date time value, then parse as Time.
20
- if value =~ /\b[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]*Z\b/ # if the value is UTC formatted
21
- line << "\"#{Time.parse(value)}\""
22
- # if its the last value in the row then dont put a comma at the end.
23
- elsif value == row[-1]
24
- line <<"\"#{value}\""
25
- # not the last value in the row, throw a comma after the value
26
- else
27
- line << "\"#{value}\","
28
- end
29
- end
30
- # append this line to the csv
31
- file.write(line + "\n")
32
- # increment the row number
33
- row_num += 1
34
- end
35
- end
36
- end
@@ -1,48 +0,0 @@
1
- require_relative "lib/d2l_sdk"
2
- require 'benchmark'
3
-
4
- #ap get_org_unit_sections(23434)
5
- =begin
6
- courses = get_all_courses
7
- #ap courses[0..30]
8
- courses[0..300].each do |course|
9
- section = get_org_unit_sections(course["Identifier"])
10
- if section != nil
11
- puts section
12
- break
13
- end
14
- end
15
- =end
16
- ap get_org_unit_properties(6840)
17
-
18
-
19
- #search_var = "test"
20
- #ap get_courses_by_code(search_var)
21
- #n = get_courses_by_code_(search_var)
22
-
23
-
24
- #ASSUMPTION: get_all_courses is slower.
25
- #m = []
26
- #get_courses_by_code(search_var).each do |course|
27
- # course.delete("Path")
28
- # m.push(course)
29
- #end
30
- #ap get_all_courses.size
31
- #ap get_all_courses_["Items"]
32
-
33
- #puts "get_courses_by_code_ has #{n.size - m.size} more courses returned than its alternative"
34
- #puts "Courses not included in get_courses_by_code_"
35
- #ap m - n
36
- #puts "Courses not included in get_courses_by_code"
37
- #ap n - m
38
-
39
- #Benchmark.bmbm do |x|
40
- # x.report("get_courses_by_code_"){ap get_courses_by_code_(search_var).size}
41
- # x.report("get_all_paged_courses_by_code"){ap get_all_paged_courses_by_code(search_var).size}
42
- #end
43
- =begin
44
- Benchmark.bmbm do |x|
45
- x.report("get_all_courses"){get_all_courses}
46
- x.report("get_all_courses_"){get_all_courses_}
47
- end
48
- =end
@@ -1,106 +0,0 @@
1
- require_relative 'd2l_api'
2
-
3
- # Test1
4
- def test_roles_viewing
5
- puts '[-] Test1: Checking if Current User can check existing roles'.cyan
6
- path = '/d2l/api/lp/1.4/roles/'
7
- http_method = 'GET'
8
- test_uri = create_authenticated_uri(path, http_method)
9
- puts '[-] Authenticated URI Created For: ' + path
10
- # General Brightspace API response behaviors
11
- get_query(test_uri)
12
- puts "[+] Test1 Succeeded\n".green
13
- rescue
14
- puts '[!] Test1 failed'.red
15
- end
16
-
17
- # Test 2
18
- def test_whoami
19
- puts "[-] Test2: Getting Current User's WhoAmI".cyan
20
- path = '/d2l/api/lp/1.4/users/whoami'
21
- http_method = 'GET'
22
- test_uri = create_authenticated_uri(path, http_method)
23
- puts '[-] Authenticated URI Created: ' + path
24
- # General Brightspace API response behaviors
25
- get_query(test_uri)
26
- puts "[+] Test2 Succeeded\n".green
27
- rescue
28
- puts '[!] Test2 failed'.red
29
- end
30
-
31
- # TEST 3
32
- def test_create_user
33
- puts '[-] Test3: testing create sample user data function'.cyan
34
- payload = { 'OrgDefinedId' => '', # String
35
- 'FirstName' => 'TestUser', # String
36
- 'MiddleName' => 'Test', # String
37
- 'LastName' => 'Test', # String
38
- 'ExternalEmail' => nil, # String (nil or well-formed email addr)
39
- 'UserName' => 'test12345abcd', # String
40
- 'RoleId' => 110, # number
41
- 'IsActive' => false, # bool
42
- 'SendCreationEmail' => false, # bool
43
- }
44
- create_user_data(payload)
45
- puts "[+] Test3 Succeeded\n".green
46
- rescue => error
47
- ap error
48
- puts "[!] Test3 failed\n".red
49
- # ap error.backtrace
50
- end
51
-
52
- # Test 4
53
- def test_update_user
54
- puts '[-] Test4: testing update sample user data function'.cyan
55
- newData = {
56
- 'OrgDefinedId' => '',
57
- 'FirstName' => 'TestUser',
58
- 'MiddleName' => 'changedName',
59
- 'LastName' => 'Test',
60
- 'ExternalEmail' => nil,
61
- 'UserName' => 'test12345a',
62
- 'Activation' => {
63
- 'IsActive' => false
64
- }
65
- }
66
- update_user_data(48558, newData) # TCP failed?
67
- puts "[+] Test4 Succeeded\n".green
68
- rescue => error
69
- ap error
70
- ap error.backtrace
71
- puts "[!] Test4 failed\n".red
72
- end
73
-
74
- # Test 5
75
- def test_who_another_user_is(id)
76
- puts "[-] Test5: Getting Current User's Ability to view other user profiles".cyan
77
- path = '/d2l/api/lp/1.4/users/' + id # '/d2l/api/lp/1.4/users/whoami'
78
- http_method = 'GET'
79
- test_uri = create_authenticated_uri(path, http_method)
80
- puts '[-] Authenticated URI Created: ' + path
81
- # General Brightspace API response behaviors
82
- get_query(test_uri)
83
- puts "[+] Test5 Succeeded\n".green
84
- rescue
85
- puts '[!] Test5 failed'.red
86
- end
87
-
88
- def test_search_by_username(username)
89
- puts '[-] Test6: Using brightspace search functionality by UserName'.cyan
90
- path = '/d2l/api/lp/1.4/users/' + '?UserName=' + username
91
- http_method = 'GET'
92
- test_uri = create_authenticated_uri(path, http_method)
93
- puts '[-] Searching for: ' + username
94
- # General Brightspace API response behaviors
95
- get_query(test_uri)
96
- puts "[+] Test6 Succeeded\n".green
97
- rescue
98
- puts '[!] Test6 failed'.red
99
- end
100
-
101
- # test_roles_viewing
102
- # test_whoami
103
- test_create_user
104
- # test_update_user
105
- # test_who_another_user_is('47906')
106
- # test_search_by_username('test12345abc')