Project

General

Profile

« Previous | Next » 

Revision 2244

Added by sgarg over 20 years ago

Changed AccessControlTest.java to include new tests

View differences:

test/edu/ucsb/nceas/metacattest/AccessControlTest.java
25 25

  
26 26
package edu.ucsb.nceas.metacattest;
27 27

  
28
import java.io.FileReader;
29
import java.io.IOException;
30 28
import java.io.Reader;
31 29
import java.io.StringReader;
32 30
import java.util.Calendar;
......
45 43
import junit.framework.Test;
46 44
import junit.framework.TestCase;
47 45
import junit.framework.TestSuite;
46
import java.io.File;
48 47

  
49 48
/**
50
 * A JUnit test for testing Step class processing
49
 * A JUnit test for testing Access Control in Metacat
51 50
 */
52 51
public class AccessControlTest
53 52
    extends TestCase {
53

  
54 54
  private String metacatUrl = "@systemidserver@@servlet-path@";
55
  private String wrongMetacatUrl =
56
      "http://somepalce.somewhere.com/some/servlet/metacat";
57 55
  private String username = "@mcuser@";
58 56
  private String password = "@mcpassword@";
59 57
  private String anotheruser = "@mcanotheruser@";
60 58
  private String anotherpassword = "@mcanotherpassword@";
61
  private String failpass = "uidfnkj43987yfdn";
62 59
  private String prefix = "test";
63 60
  private String newdocid = null;
64
  private String testfile = "test/jones.204.22.xml";
65
  private String queryFile = "test/query.xml";
61
  private String onlineDocid = null;
66 62
  private String testdocument = "";
63
  private String onlinetestdatafile1 = "test/onlineDataFile1";
64
  private String onlinetestdatafile2 = "test/onlineDataFile2";
65

  
67 66
  private Metacat m;
67

  
68 68
  private boolean SUCCESS = true;
69 69
  private boolean FAILURE = false;
70 70

  
71

  
72

  
71 73
  /**
74
   * These variables are for eml-2.0.1 only. For other eml versions,
75
   * this function might have to modified
76
   */
77

  
78
  private String testEmlHeader =
79
      "<?xml version=\"1.0\"?><eml:eml" +
80
      " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
81
      " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
82
      " packageId=\"eml.1.1\" system=\"knb\"" +
83
      " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
84
      " scope=\"system\">";
85

  
86
  private String testEmlCreatorBlock =
87
      "<creator scope=\"document\">                                       " +
88
      " <individualName>                                                  " +
89
      "    <surName>Smith</surName>                                       " +
90
      " </individualName>                                                 " +
91
      "</creator>                                                         ";
92

  
93
  private String testEmlContactBlock =
94
      "<contact scope=\"document\">                                       " +
95
      " <individualName>                                                  " +
96
      "    <surName>Jackson</surName>                                     " +
97
      " </individualName>                                                 " +
98
      "</contact>                                                         ";
99

  
100
  private String testEmlInlineBlock1 =
101
      "<inline>                                                           " +
102
      "  <admin>                                                          " +
103
      "    <contact>                                                      " +
104
      "      <name>Operator</name>                                        " +
105
      "      <institution>PSI</institution>                               " +
106
      "    </contact>                                                     " +
107
      "  </admin>                                                         " +
108
      "</inline>                                                          ";
109

  
110
  private String testEmlInlineBlock2 =
111
      "<inline>                                                           " +
112
      "  <instrument>                                                     " +
113
      "    <instName>LCQ</instName>                                       " +
114
      "    <source type=\"ESI\"></source>                                 " +
115
      "    <detector type=\"EM\"></detector>                              " +
116
      "  </instrument>                                                    " +
117
      "</inline>                                                          ";
118

  
119
  /**
120
    * This function returns a valid eml document with no access rules
121
    * This function is for eml-2.0.1 only. For other eml versions,
122
    * this function might have to modified
123
    */
124
   private String getTestEmlDoc(String title,  String inlineData1,
125
                               String inlineData2, String onlineUrl1,
126
                               String onlineUrl2){
127
    String testDocument = "";
128
    testDocument = testDocument + testEmlHeader +
129
        "<dataset scope=\"document\"><title>" + title + "</title>" +
130
        testEmlCreatorBlock;
131

  
132
    if(inlineData1 != null){
133
      testDocument = testDocument + "<distribution scope=\"document\">" +
134
          inlineData1 + "</distribution>";
135
    }
136
    if(inlineData2 != null){
137
      testDocument = testDocument + "<distribution scope=\"document\">" +
138
          inlineData2 + "</distribution>";
139
    }
140
    if(onlineUrl1 != null){
141
      testDocument = testDocument + "<distribution scope=\"document\">" +
142
          "<online><url function=\"download\">" + onlineUrl1 + "</url></online></distribution>";
143
    }
144
    if(onlineUrl2 != null){
145
      testDocument = testDocument + "<distribution scope=\"document\">" +
146
          "<online><url function=\"download\">" + onlineUrl2 + "</url></online></distribution>";
147
    }
148
    testDocument = testDocument + testEmlContactBlock + "</dataset></eml:eml>";
149

  
150
    return testDocument;
151
  }
152

  
153
  /**
72 154
   * Constructor to build the test
73 155
   *
74 156
   * @param name the name of the test method
......
83 165
   */
84 166
  public void setUp() {
85 167
    try {
86
      FileReader fr = new FileReader(testfile);
87
      testdocument = IOUtil.getAsString(fr, true);
88
    }
89
    catch (IOException ioe) {
90
      fail("Can't read test data to run the test: " + testfile);
91
    }
92

  
93
    try {
94 168
      System.err.println("Test Metacat: " + metacatUrl);
95 169
      m = MetacatFactory.createMetacatConnection(metacatUrl);
96 170
    }
......
113 187
    TestSuite suite = new TestSuite();
114 188
    suite.addTest(new AccessControlTest("initialize"));
115 189
    // Test basic functions
116
    suite.addTest(new AccessControlTest("login"));
117
    suite.addTest(new AccessControlTest("insert"));
118
    suite.addTest(new AccessControlTest("read"));
119
    suite.addTest(new AccessControlTest("update"));
120
    suite.addTest(new AccessControlTest("delete"));
190
    //suite.addTest(new AccessControlTest("login"));
191
    //suite.addTest(new AccessControlTest("insert"));
192
    //suite.addTest(new AccessControlTest("read"));
193
    //suite.addTest(new AccessControlTest("update"));
194
    //suite.addTest(new AccessControlTest("delete"));
121 195
    // Tests when no access is specified
122 196
    suite.addTest(new AccessControlTest("noAccessSpecified_Owner"));
123
    suite.addTest(new AccessControlTest("noAccessSpecified_Random"));
124
    suite.addTest(new AccessControlTest("noAccessSpecified_Public"));
197
    //suite.addTest(new AccessControlTest("noAccessSpecified_Random"));
198
    //suite.addTest(new AccessControlTest("noAccessSpecified_Public"));
125 199

  
126 200
    return suite;
127 201
  }
......
162 236
   */
163 237
  public void insert() {
164 238
    try {
239
      testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
240
                                   null, null, null);
165 241
      String identifier = newdocid + ".1";
166 242
      m.login(username, password);
167 243
      String response = m.insert(identifier,
......
169 245
      assertTrue(response.indexOf("<success>") != -1);
170 246
      assertTrue(response.indexOf(identifier) != -1);
171 247
      System.err.println(response);
172

  
173 248
    }
174 249
    catch (MetacatAuthException mae) {
175 250
      fail("Authorization failed:\n" + mae.getMessage());
......
194 269
   */
195 270
  public void read() {
196 271
    try {
272
      testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1, null,
273
                                   null, null);
197 274
      m.login(username, password);
198 275
      Reader r = m.read(newdocid + ".1");
199 276
      String doc = IOUtil.getAsString(r, true);
200
      doc = doc + "\n";
201
      System.err.println(doc);
202 277
      assertTrue(doc.equals(testdocument));
203 278
    }
204 279
    catch (MetacatAuthException mae) {
......
217 292
   */
218 293
  public void update() {
219 294
    try {
295
      testdocument = getTestEmlDoc("Testing update", testEmlInlineBlock1, null,
296
                                   null, null);
220 297
      String identifier = newdocid + ".2";
221 298
      m.login(username, password);
222 299
      String response = m.update(identifier,
......
272 349
    }
273 350
  }
274 351

  
275
  /**
352

  
353

  
354
  /** *********
276 355
   * Test the case when no access is specified and owner is logged in
356
   * Cases being checked:
357
   * 1. the owner tries to read the document  - Success.
358
   *                                            Both document and data show up.
359
   * 2. the owner tries to update the document - Success.
360
   * 3. the owner tries to update the inline data - Success.
361
   * 4. the owner tries to update the online data file - Success.
362
   * 5. the owner tries to update the access rules for the document - Success.
363
   * 6. the owner tries to update the access rules for the inline data - Success.
364
   * 7. the owner tries to update the access rules for the online data - Success.
365
   * 8. the owner tries to delete the document - Success.
277 366
   */
278
  public void noAccessRuleSpecified_Owner() {
367
  public void noAccessSpecified_Owner() {
279 368
    try {
280 369
      newdocid = generateDocid();
281
      String identifier = newdocid + ".1";
370

  
282 371
      // login
283 372
      m.login(username, password);
284 373

  
374
      // upload online document
375
      onlineDocid = generateDocid();
376
      uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS);
377

  
285 378
      // insert a document - get the docid
379
      testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
380
                                   null, "ecogrid://knb/" + onlineDocid, null);
286 381
      insertDocid(newdocid + ".1", testdocument, SUCCESS);
287
      readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS);
382
      readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS);
288 383

  
289 384
      // update the document
385
      testdocument = getTestEmlDoc("Testing update", testEmlInlineBlock1,
386
                                   null, null, null);
290 387
      updateDocid(newdocid + ".2", testdocument, SUCCESS);
291
      readDocidWhichEqualsDoc(newdocid + ".2", testdocument, SUCCESS);
388
      readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS);
292 389

  
293 390
      // update the inline data
391
      testdocument = getTestEmlDoc("Testing update inline", testEmlInlineBlock2,
392
                                   null, null, null);
294 393
      updateDocid(newdocid + ".3", testdocument, SUCCESS);
295
      readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS);
394
      readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS);
296 395

  
297 396
      // update the online data
298
      updateDocid(newdocid + ".4", testdocument, SUCCESS);
299
      readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS);
397
      uploadDocid(onlineDocid + ".2", onlinetestdatafile2, SUCCESS);
300 398

  
301 399
      // update the document access control
302
      updateDocid(newdocid + ".5", testdocument, SUCCESS);
303
      readDocidWhichEqualsDoc(newdocid + ".5", testdocument, SUCCESS);
400
      //updateDocid(newdocid + ".5", testdocument, SUCCESS);
401
      //readDocidWhichEqualsDoc(newdocid + ".5", testdocument, SUCCESS);
304 402

  
305 403
      // update the document access control for inline data
306
      updateDocid(newdocid + ".6", testdocument, SUCCESS);
307
      readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS);
404
      //updateDocid(newdocid + ".6", testdocument, SUCCESS);
405
      //readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS);
308 406

  
309 407
      // update the document access control for online data
310
      updateDocid(newdocid + ".7", testdocument, SUCCESS);
311
      readDocidWhichEqualsDoc(newdocid + ".7", testdocument, SUCCESS);
408
      //updateDocid(newdocid + ".7", testdocument, SUCCESS);
409
      //readDocidWhichEqualsDoc(newdocid + ".7", testdocument, SUCCESS);
312 410

  
313 411
      // delete the document
314
      deleteDocid(newdocid + ".7", SUCCESS);
412
      deleteDocid(newdocid, SUCCESS);
315 413
      m.logout();
316 414
    }
317 415
    catch (MetacatAuthException mae) {
......
326 424

  
327 425
  }
328 426

  
329
  /**
427

  
428

  
429
  /** *********
330 430
   * Test the case when no access is specified and another user is logged in
431
   * Cases being checked:
432
   * 1. the owner tries to read the document - Failure.
433
   * 2. the owner tries to update the document - Failure.
434
   * 3. the owner tries to update the inline data - Failure.
435
   * 4. the owner tries to update the online data file - Failure.
436
   * 5. the owner tries to update the access rules for the document - Failure.
437
   * 6. the owner tries to update the access rules for the inline data - Failure.
438
   * 7. the owner tries to update the access rules for the online data - Failure.
439
   * 8. the owner tries to delete the document - Failure.
331 440
   */
332
  public void noAccessRuleSpecified_Random() {
441
  public void noAccessSpecified_Random() {
333 442
    try {
334 443
      newdocid = generateDocid();
335 444
      String identifier = newdocid + ".1";
......
384 493

  
385 494
  }
386 495

  
387
  /**
496

  
497

  
498
  /** *********
388 499
   * Test the case when no access is specified and public is logged in
500
   * Cases being checked:
501
   * 1. the owner tries to read the document - Failure.
502
   * 2. the owner tries to update the document - Failure.
503
   * 3. the owner tries to update the inline data - Failure.
504
   * 4. the owner tries to update the online data file - Failure.
505
   * 5. the owner tries to update the access rules for the document - Failure.
506
   * 6. the owner tries to update the access rules for the inline data - Failure.
507
   * 7. the owner tries to update the access rules for the online data - Failure.
508
   * 8. the owner tries to delete the document - Failure.
389 509
   */
390
  public void noAccessRuleSpecified_Public() {
510
  public void noAccessSpecified_Public() {
391 511
    try {
392 512
      newdocid = generateDocid();
393 513
      String identifier = newdocid + ".1";
......
441 561

  
442 562
  }
443 563

  
564

  
565
  /**
566
   * Insert a document into metacat. The expected result is passed as result
567
   */
568

  
444 569
  public String insertDocid(String docid, String docText, boolean result) {
445 570
    String response = null;
446 571
    try {
......
470 595
    return response;
471 596
  }
472 597

  
598

  
599
  /**
600
   * Insert a document into metacat. The expected result is passed as result
601
   */
602

  
603
  public String uploadDocid(String docid, String filePath, boolean result) {
604
    String response = null;
605
    try {
606
      response = m.upload(docid, new File(filePath));
607

  
608
      if (result) {
609
        assertTrue( (response.indexOf("<success>") != -1));
610
        assertTrue(response.indexOf(docid) != -1);
611
      }
612
      else {
613
        assertTrue( (response.indexOf("<success>") == -1));
614
      }
615
      System.err.println(response);
616
    }
617
    catch (MetacatInaccessibleException mie) {
618
      fail("Metacat Inaccessible:\n" + mie.getMessage());
619
    }
620
    catch (InsufficientKarmaException ike) {
621
      fail("Insufficient karma:\n" + ike.getMessage());
622
    }
623
    catch (MetacatException me) {
624
      fail("Metacat Error:\n" + me.getMessage());
625
    }
626
    catch (Exception e) {
627
      fail("General exception:\n" + e.getMessage());
628
    }
629
    return response;
630
  }
631

  
632
  /**
633
   * Update a document in metacat. The expected result is passed as result
634
   */
473 635
  public String updateDocid(String docid, String docText, boolean result) {
474 636
    String response = null;
475 637
    try {
476 638
      response = m.update(docid,
477
                          new StringReader(docText), null);
639
                          new StringReader(testdocument), null);
640

  
478 641
      if (result) {
479 642
        assertTrue( (response.indexOf("<success>") != -1));
480 643
        assertTrue(response.indexOf(docid) != -1);
......
500 663
    return response;
501 664
  }
502 665

  
666
  /**
667
   * Delete a document into metacat. The expected result is passed as result
668
   */
503 669
  public void deleteDocid(String docid, boolean result) {
504 670
    try {
505 671
      String response = m.delete(docid);
......
525 691
    }
526 692
  }
527 693

  
694
  /**
695
   * Read a document from metacat and check if it is equal to a given string.
696
   * The expected result is passed as result
697
   */
698

  
528 699
  public void readDocidWhichEqualsDoc(String docid, String testDoc, boolean result) {
529 700
    try {
530 701
      Reader r = m.read(docid);
531 702
      String doc = IOUtil.getAsString(r, true);
532
      doc = doc + "\n";
533 703
      if (result) {
534 704
        assertTrue(testDoc.equals(doc));
535 705
      } else {

Also available in: Unified diff