Project

General

Profile

« Previous | Next » 

Revision 6320

tests for isAuthorized and setAccessPolicy

View differences:

test/edu/ucsb/nceas/metacat/dataone/CNodeServiceTest.java
41 41
import org.dataone.service.Constants;
42 42
import org.dataone.service.exceptions.InsufficientResources;
43 43
import org.dataone.service.exceptions.InvalidRequest;
44
import org.dataone.service.exceptions.NotAuthorized;
44 45
import org.dataone.service.exceptions.NotFound;
45 46
import org.dataone.service.exceptions.NotImplemented;
46 47
import org.dataone.service.exceptions.ServiceFailure;
......
114 115
		suite.addTest(new CNodeServiceTest("testGetFormat"));
115 116
		//suite.addTest(new CNodeServiceTest("testGetLogRecords"));
116 117
		suite.addTest(new CNodeServiceTest("testGetSystemMetadata"));
118
		suite.addTest(new CNodeServiceTest("testIsAuthorized"));
117 119
		suite.addTest(new CNodeServiceTest("testListFormats"));
118 120
		suite.addTest(new CNodeServiceTest("testListNodes"));
119 121
		suite.addTest(new CNodeServiceTest("testObjectFormatNotFoundException"));
......
122 124
		suite.addTest(new CNodeServiceTest("testReplicationStatus"));
123 125
		suite.addTest(new CNodeServiceTest("testReserveIdentifier"));
124 126
		suite.addTest(new CNodeServiceTest("testSearch"));
127
		suite.addTest(new CNodeServiceTest("testSetAccessPolicy"));
125 128
		suite.addTest(new CNodeServiceTest("testSetOwner"));
126 129
	
127 130
		return suite;
......
373 376
        }
374 377
	}
375 378
	
379
	public void testSetAccessPolicy() {
380
	    printTestHeader("testSetAccessPolicy");
381

  
382
	    try {
383
            Session session = getTestSession();
384
			Identifier guid = new Identifier();
385
			guid.setValue("testSetAccessPolicy." + System.currentTimeMillis());
386
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
387
			// save it
388
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
389
			assertTrue(result);
390
			AccessPolicy accessPolicy = new AccessPolicy();
391
			AccessRule accessRule = new AccessRule();
392
			accessRule.addPermission(Permission.WRITE);
393
			Subject publicSubject = new Subject();
394
			publicSubject.setValue(Constants.PUBLIC_SUBJECT);
395
			accessRule.addSubject(publicSubject);
396
			accessPolicy.addAllow(accessRule);
397
			// set it
398
			result = CNodeService.getInstance().setAccessPolicy(session, guid, accessPolicy );
399
			assertTrue(result);
400
			// check it
401
			result = CNodeService.getInstance().isAuthorized(session, guid, Permission.WRITE);
402
			assertTrue(result);
403
        } catch(Exception e) {
404
            fail("Unexpected error: " + e.getMessage());
405
        }
406
	}
407
	
408
	public void testIsAuthorized() {
409
	    printTestHeader("testIsAuthorized");
410

  
411
	    try {
412
            Session session = getTestSession();
413
			Identifier guid = new Identifier();
414
			guid.setValue("testIsAuthorized." + System.currentTimeMillis());
415
			SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject());
416
			// save it
417
			boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, sysmeta);
418
			assertTrue(result);
419
			// check it
420
			Subject publicSubject = new Subject();
421
			publicSubject.setValue(Constants.PUBLIC_SUBJECT);
422
			session.setSubject(publicSubject);
423
			// public read
424
			result = CNodeService.getInstance().isAuthorized(session, guid, Permission.READ);
425
			assertTrue(result);
426
			// not public write
427
			try {
428
				result = false;
429
				result = CNodeService.getInstance().isAuthorized(session, guid, Permission.WRITE);
430
				fail("Public WRITE should be denied");
431
			} catch (NotAuthorized nae) {
432
				result = true;
433
			}
434
			assertTrue(result);
435
        } catch(Exception e) {
436
            fail("Unexpected error: " + e.getMessage());
437
        }
438
	}
439
	
376 440
	public void testReplicationPolicy() {
377 441
	    printTestHeader("testReplicationPolicy");
378 442

  

Also available in: Unified diff