|
15 | 15 | # under the License.
|
16 | 16 |
|
17 | 17 | from __future__ import absolute_import
|
18 |
| -from splunklib.binding import HTTPError |
| 18 | +from splunklib.binding import HTTPError, namespace |
19 | 19 | from tests import testlib
|
20 | 20 | import logging
|
21 | 21 |
|
@@ -264,7 +264,7 @@ def query():
|
264 | 264 | self.assertRaisesRegex(HTTPError, "value must be smaller that value2", query)
|
265 | 265 |
|
266 | 266 |
|
267 |
| -# This test makes sure that the endpoint we use for macros (configs/conf-macros) |
| 267 | +# This test makes sure that the endpoint we use for macros (data/macros/) |
268 | 268 | # does not require admin privileges and can be used by normal users.
|
269 | 269 | class TestPrivileges(testlib.SDKTestCase):
|
270 | 270 | macro_name = "SDKTestMacro"
|
@@ -318,6 +318,67 @@ def test_create_macro_no_admin(self):
|
318 | 318 | self.assertEqual(out[0]["test"], "123")
|
319 | 319 |
|
320 | 320 |
|
| 321 | +# This test makes sure that the endpoint we use for macros (data/macros/) |
| 322 | +# does not require admin privileges and can be used by normal users. |
| 323 | +class TestPrivilegesWithNamespace(testlib.SDKTestCase): |
| 324 | + macro_name = "SDKTestMacro" |
| 325 | + username = "SDKTestMacroUser".lower() |
| 326 | + password = "SDKTestMacroUserPassword!" |
| 327 | + |
| 328 | + # TODO: Would be nice to create app on-demand here and control the |
| 329 | + # permissions, so that we are not dependent on default settings. |
| 330 | + namespace = namespace(owner="nobody", app="launcher") |
| 331 | + |
| 332 | + def setUp(self): |
| 333 | + testlib.SDKTestCase.setUp(self) |
| 334 | + self.cleanUsers() |
| 335 | + |
| 336 | + self.service.users.create( |
| 337 | + username=self.username, password=self.password, roles=["power"] |
| 338 | + ) |
| 339 | + |
| 340 | + self.service.logout() |
| 341 | + kwargs = self.opts.kwargs.copy() |
| 342 | + kwargs["username"] = self.username |
| 343 | + kwargs["password"] = self.password |
| 344 | + self.service = client.connect(**kwargs) |
| 345 | + |
| 346 | + self.cleanMacros() |
| 347 | + |
| 348 | + def tearDown(self): |
| 349 | + testlib.SDKTestCase.tearDown(self) |
| 350 | + self.cleanMacros() |
| 351 | + self.service = client.connect(**self.opts.kwargs) |
| 352 | + self.cleanUsers() |
| 353 | + |
| 354 | + def cleanUsers(self): |
| 355 | + for user in self.service.users: |
| 356 | + if user.name == self.username: |
| 357 | + self.service.users.delete(self.username) |
| 358 | + |
| 359 | + def cleanMacros(self): |
| 360 | + for macro in self.service.macros: |
| 361 | + if macro.name == self.macro_name: |
| 362 | + self.service.macros.delete(self.macro_name, namespace=self.namespace) |
| 363 | + |
| 364 | + def test_create_macro_no_admin(self): |
| 365 | + self.service.macros.create( |
| 366 | + self.macro_name, 'eval test="123"', namespace=self.namespace |
| 367 | + ) |
| 368 | + |
| 369 | + stream = self.service.jobs.oneshot( |
| 370 | + f"| makeresults count=1 | `{self.macro_name}`", |
| 371 | + output_mode="json", |
| 372 | + namespace=self.namespace, |
| 373 | + ) |
| 374 | + |
| 375 | + result = results.JSONResultsReader(stream) |
| 376 | + out = list(result) |
| 377 | + |
| 378 | + self.assertTrue(len(out) == 1) |
| 379 | + self.assertEqual(out[0]["test"], "123") |
| 380 | + |
| 381 | + |
321 | 382 | if __name__ == "__main__":
|
322 | 383 | try:
|
323 | 384 | import unittest2 as unittest
|
|
0 commit comments