Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author: sgarg $'
8
 *     '$Date: 2004-10-18 14:42:37 -0700 (Mon, 18 Oct 2004) $'
9
 * '$Revision: 2328 $'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25

    
26
package edu.ucsb.nceas.metacattest;
27

    
28
import java.io.Reader;
29
import java.io.StringReader;
30
import java.util.Calendar;
31
import java.util.Date;
32
import java.util.GregorianCalendar;
33
import java.util.SimpleTimeZone;
34
import java.util.TimeZone;
35

    
36
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
37
import edu.ucsb.nceas.metacat.client.Metacat;
38
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
39
import edu.ucsb.nceas.metacat.client.MetacatException;
40
import edu.ucsb.nceas.metacat.client.MetacatFactory;
41
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
42
import edu.ucsb.nceas.utilities.IOUtil;
43
import junit.framework.Test;
44
import junit.framework.TestCase;
45
import junit.framework.TestSuite;
46
import java.io.File;
47

    
48
/**
49
 * A JUnit test for testing Access Control for online data in Metacat
50
 */
51
public class OnlineDataAccessTest
52
    extends TestCase {
53

    
54
    private String metacatUrl = "@systemidserver@@servlet-path@";
55
    private String username = "@mcuser@";
56
    private String password = "@mcpassword@";
57
    private String anotheruser = "@mcanotheruser@";
58
    private String anotherpassword = "@mcanotherpassword@";
59
    private String prefix = "test";
60
    private String newdocid = null;
61
    private String onlineDocid = null;
62
    private String testdocument = "";
63
    private String onlinetestdatafile1 = "test/onlineDataFile1";
64
    private String onlinetestdatafile2 = "test/onlineDataFile2";
65

    
66
    private Metacat m;
67

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

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

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

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

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

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

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

    
117
    /**
118
     * This function returns an access block based on the params passed
119
     */
120
    private String getAccessBlock(String principal, boolean grantAccess,
121
                                  boolean read, boolean write,
122
                                  boolean changePermission, boolean all) {
123
        String accessBlock = "<access " +
124
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
125
            " order=\"allowFirst\" scope=\"document\">";
126

    
127
        if (grantAccess) {
128
            accessBlock += "<allow>";
129
        }
130
        else {
131
            accessBlock += "<deny>";
132
        }
133

    
134
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
135

    
136
        if (all) {
137
            accessBlock += "<permission>all</permission>";
138
        }
139
        else {
140
            if (read) {
141
                accessBlock += "<permission>read</permission>";
142
            }
143
            if (write) {
144
                accessBlock += "<permission>write</permission>";
145
            }
146
            if (changePermission) {
147
                accessBlock += "<permission>changePermission</permission>";
148
            }
149
        }
150

    
151
        if (grantAccess) {
152
            accessBlock += "</allow>";
153
        }
154
        else {
155
            accessBlock += "</deny>";
156
        }
157
        accessBlock += "</access>";
158

    
159
        return accessBlock;
160

    
161
    }
162

    
163
    /**
164
     * This function returns a valid eml document with no access rules
165
     * This function is for eml-2.0.1 only. For other eml versions,
166
     * this function might have to modified
167
     */
168
    private String getTestEmlDoc(String title, String inlineData1,
169
                                 String inlineData2, String onlineUrl1,
170
                                 String onlineUrl2, String docAccessBlock,
171
                                 String inlineAccessBlock1,
172
                                 String inlineAccessBlock2,
173
                                 String onlineAccessBlock1,
174
                                 String onlineAccessBlock2) {
175

    
176
        String testDocument = "";
177
        testDocument = testDocument + testEmlHeader +
178
            "<dataset scope=\"document\"><title>" + title + "</title>" +
179
            testEmlCreatorBlock;
180

    
181
        if (inlineData1 != null) {
182
            testDocument = testDocument
183
                + "<distribution scope=\"document\" id=\"inlineEntity1\">"
184
                + inlineData1 + "</distribution>";
185
        }
186
        if (inlineData2 != null) {
187
            testDocument = testDocument
188
                + "<distribution scope=\"document\" id=\"inlineEntity2\">"
189
                + inlineData2 + "</distribution>";
190
        }
191
        if (onlineUrl1 != null) {
192
            testDocument = testDocument
193
                + "<distribution scope=\"document\" id=\"onlineEntity1\">"
194
                + "<online><url function=\"download\">"
195
                + onlineUrl1 + "</url></online></distribution>";
196
        }
197
        if (onlineUrl2 != null) {
198
            testDocument = testDocument +
199
                "<distribution scope=\"document\" id=\"onlineEntity2\">"
200
                + "<online><url function=\"download\">"
201
                + onlineUrl2 + "</url></online></distribution>";
202
        }
203
        testDocument += testEmlContactBlock;
204

    
205
        if (docAccessBlock != null) {
206
            testDocument += docAccessBlock;
207
        }
208

    
209
        testDocument += "</dataset>";
210

    
211
        if (inlineAccessBlock1 != null) {
212
            testDocument += "<additionalMetadata>";
213
            testDocument += "<describes>inlineEntity1</describes>";
214
            testDocument += inlineAccessBlock1;
215
            testDocument += "</additionalMetadata>";
216
        }
217

    
218
        if (inlineAccessBlock2 != null) {
219
            testDocument += "<additionalMetadata>";
220
            testDocument += "<describes>inlineEntity2</describes>";
221
            testDocument += inlineAccessBlock2;
222
            testDocument += "</additionalMetadata>";
223
        }
224

    
225
        if (onlineAccessBlock1 != null) {
226
            testDocument += "<additionalMetadata>";
227
            testDocument += "<describes>onlineEntity1</describes>";
228
            testDocument += onlineAccessBlock1;
229
            testDocument += "</additionalMetadata>";
230
        }
231

    
232
        if (onlineAccessBlock2 != null) {
233
            testDocument += "<additionalMetadata>";
234
            testDocument += "<describes>onlineEntity2</describes>";
235
            testDocument += onlineAccessBlock2;
236
            testDocument += "</additionalMetadata>";
237
        }
238

    
239
        testDocument += "</eml:eml>";
240

    
241
        //System.out.println("Returning following document" + testDocument);
242
        return testDocument;
243
    }
244

    
245
    /**
246
     * Constructor to build the test
247
     *
248
     * @param name the name of the test method
249
     */
250
    public OnlineDataAccessTest(String name) {
251
        super(name);
252
        newdocid = generateDocid();
253
    }
254

    
255
    /**
256
     * Establish a testing framework by initializing appropriate objects
257
     */
258
    public void setUp() {
259
        try {
260
            System.err.println("Test Metacat: " + metacatUrl);
261
            m = MetacatFactory.createMetacatConnection(metacatUrl);
262
        }
263
        catch (MetacatInaccessibleException mie) {
264
            System.err.println("Metacat is: " + metacatUrl);
265
            fail("Metacat connection failed." + mie.getMessage());
266
        }
267
    }
268

    
269
    /**
270
     * Release any objects after tests are complete
271
     */
272
    public void tearDown() {
273
    }
274

    
275
    /**
276
     * Create a suite of tests to be run together
277
     */
278
    public static Test suite() {
279
        TestSuite suite = new TestSuite();
280
        suite.addTest(new OnlineDataAccessTest("initialize"));
281

    
282
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_1"));
283
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_2"));
284
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_3"));
285
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_4"));
286
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_5"));
287
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_6"));
288

    
289
        return suite;
290
    }
291

    
292

    
293

    
294
    /**
295
     * Run an initial test that always passes to check that the test
296
     * harness is working.
297
     */
298
    public void initialize() {
299
        assertTrue(1 == 1);
300
    }
301

    
302

    
303
    /** *********
304
     * Checking the following cases:
305
     * when only online data is uploaded by a user and
306
     * -> he tries to read it  - success
307
     * -> he tries to add same docid again  - failure
308
     * -> he tries to update it  - success
309
     * -> he tries to set permissions on it  - success
310
     * -> he tries to delete it  - success
311
     * -> he tries to read it after deleteing - failure
312
     */
313
    public void onlineDataCasesTest_1() {
314
        try {
315

    
316
            // upload online data
317
            onlineDocid = generateDocid();
318
            m.login(username, password);
319
            uploadDocid(onlineDocid + ".1",
320
                        onlinetestdatafile1, SUCCESS, false);
321

    
322
            // try to read the data
323
            readDocid(onlineDocid + ".1", SUCCESS, false);
324

    
325
            // try to upload another data with same id
326
            uploadDocid(onlineDocid + ".1",
327
                        onlinetestdatafile2, FAILURE, false);
328

    
329
            // try to upload another data with updated id
330
            uploadDocid(onlineDocid + ".2",
331
                        onlinetestdatafile2, SUCCESS, false);
332

    
333
            // try to set the permissions for the uploaded document
334
            // the docid given is for the online document
335
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
336
                                         null, null,
337
                                         "ecogrid://knb/" + onlineDocid + ".1",
338
                                         null, getAccessBlock(anotheruser, true,
339
                true, false, false, false),
340
                                         null, null, null, null);
341
            newdocid = generateDocid();
342
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
343
            m.logout();
344

    
345
            // check if the permissions were set properly
346
            m.login(anotheruser, anotherpassword);
347
            readDocid(onlineDocid + ".1", SUCCESS, false);
348
            readDocid(onlineDocid + ".2", SUCCESS, false);
349
            m.logout();
350

    
351
            m.login(username, password);
352

    
353
            // delete the document - able to delete .1
354
            // but not able to delete .2 as no rules 
355
	    // written to access table when a document 
356
	    // is 'uploaded'
357
            deleteDocid(onlineDocid + ".1", SUCCESS, false);
358
            deleteDocid(onlineDocid + ".2", FAILURE, true);
359

    
360
            // try to read the documents now
361
            readDocid(onlineDocid + ".1", FAILURE, false);
362
            readDocid(onlineDocid + ".2", FAILURE, false);
363

    
364
            m.logout();
365

    
366
        }
367
        catch (MetacatAuthException mae) {
368
            fail("Authorization failed:\n" + mae.getMessage());
369
        }
370
        catch (MetacatInaccessibleException mie) {
371
            fail("Metacat Inaccessible:\n" + mie.getMessage());
372
        }
373
        catch (Exception e) {
374
            fail("General exception:\n" + e.getMessage());
375
        }
376
    }
377

    
378

    
379
    /** *********
380
     * Checking the following cases:
381
     * when only online data is uploaded by a user and another user
382
     * -> tries to read it  - failure
383
     * -> tries to add same docid again  - failure
384
     * -> tries to update it  - failure
385
     * -> tries to set permissions on it  - failure
386
     * -> tries to delete it  - failure
387
     */
388
    public void onlineDataCasesTest_2() {
389
        try {
390

    
391
            // upload an online document
392
            onlineDocid = generateDocid();
393
            m.login(username, password);
394
            uploadDocid(onlineDocid + ".1",
395
                        onlinetestdatafile1, SUCCESS, false);
396

    
397
            uploadDocid(onlineDocid + ".2",
398
                        onlinetestdatafile2, SUCCESS, false);
399

    
400
            // login as another user
401
            m.logout();
402
            m.login(anotheruser, anotherpassword);
403

    
404
            // try to read the data
405
            readDocid(onlineDocid + ".2", FAILURE, true);
406

    
407
            // try to upload another document with same id
408
            uploadDocid(onlineDocid + ".2",
409
                        onlinetestdatafile2, FAILURE, false);
410

    
411
            // try to upload another document with updated id
412
            uploadDocid(onlineDocid + ".3",
413
                        onlinetestdatafile2, FAILURE, true);
414

    
415

    
416
            // try to set the permissions for the uploaded document
417
            // the docid given is for the online document
418
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
419
                                         null, null,
420
                                         "ecogrid://knb/" + onlineDocid + ".1",
421
                                         "ecogrid://knb/" + onlineDocid + ".2",
422
                                         getAccessBlock(anotheruser, true,
423
                                         true, false, false, false),
424
                                         null, null, null, null);
425
            newdocid = generateDocid();
426
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
427

    
428
            // delete the document - should not be able to delete .1
429
            // but should be able to delete .2
430
            deleteDocid(onlineDocid + ".1", FAILURE, true);
431
            deleteDocid(onlineDocid + ".2", FAILURE, true);
432

    
433
            m.logout();
434

    
435
        }
436
        catch (MetacatAuthException mae) {
437
            fail("Authorization failed:\n" + mae.getMessage());
438
        }
439
        catch (MetacatInaccessibleException mie) {
440
            fail("Metacat Inaccessible:\n" + mie.getMessage());
441
        }
442
        catch (Exception e) {
443
            fail("General exception:\n" + e.getMessage());
444
        }
445
    }
446

    
447

    
448
    /** *********
449
     * Checking the following cases:
450
     * when only online data is uploaded by a user with the following different
451
     * access controls in another document
452
     *   1.read
453
     *   2.write
454
     *   3.change permission
455
     *   4.all
456
     * And another user tries to do the following:
457
     * -> tries to read it
458
     * -> tries to update it
459
     * -> tries to set permissions on it
460
     * -> tries to delete it
461
     */
462
    public void onlineDataCasesTest_3() {
463
        try {
464

    
465
            /////////Case 1./////////////////////
466
            // upload an online document - read only
467
            onlineDocid = generateDocid();
468
            m.login(username, password);
469
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
470

    
471
            // upload a document which gives read access to the online document
472
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
473
                                         null, null,
474
                                         "ecogrid://knb/" + onlineDocid + ".1",
475
                                         null, getAccessBlock(anotheruser, true,
476
                true, false, false, false),
477
                                         null, null, null, null);
478
            newdocid = generateDocid();
479
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
480
            m.logout();
481

    
482
            // login as another user
483
            m.login(anotheruser, anotherpassword);
484

    
485
            // try to read the online data
486
            readDocid(onlineDocid + ".1", SUCCESS, false);
487

    
488
            // try to upload another data with updated id
489
            uploadDocid(onlineDocid + ".2",
490
                        onlinetestdatafile2, FAILURE, true);
491

    
492
            // try to set the permissions for the uploaded document
493
            // the docid given is for the online document
494
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
495
                                         null, null,
496
                                         "ecogrid://knb/" + onlineDocid + ".1",
497
                                         null, getAccessBlock(anotheruser, true,
498
                false, false, false, true),
499
                                         null, null, null, null);
500
            newdocid = generateDocid();
501
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
502

    
503
            // delete the document
504
            deleteDocid(onlineDocid + ".1", FAILURE, true);
505
            m.logout();
506

    
507
            /////////Case 2/////////////////////
508
            // upload an online document - write only
509
            onlineDocid = generateDocid();
510
            m.login(username, password);
511
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
512

    
513
            // upload a document which gives read access to the online document
514
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Another insert",
515
                                         null, null,
516
                                         "ecogrid://knb/" + onlineDocid + ".1",
517
                                         null, getAccessBlock(anotheruser, true,
518
                false, true, false, false),
519
                                         null, null, null, null);
520
            newdocid = generateDocid();
521
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
522
            m.logout();
523

    
524
            // login as another user
525
            m.login(anotheruser, anotherpassword);
526

    
527
            // try to read the online data
528
            readDocid(onlineDocid + ".1", FAILURE, true);
529

    
530
            // try to upload another data with updated id
531
            uploadDocid(onlineDocid + ".2",
532
                        onlinetestdatafile2, SUCCESS, false);
533

    
534
            // try to set the permissions for the uploaded document
535
            // the docid given is for the online document
536
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Another insert",
537
                                         null, null,
538
                                         "ecogrid://knb/" + onlineDocid + ".1",
539
                                         null, getAccessBlock(anotheruser, true,
540
                                         false, false, false, true),
541
                                         null, null, null, null);
542
            newdocid = generateDocid();
543
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
544

    
545
            // delete the document
546
            deleteDocid(onlineDocid + ".2", FAILURE, true);
547
            m.logout();
548

    
549
            /////////Case 3/////////////////////
550
            // upload an online document - change permission only
551
            onlineDocid = generateDocid();
552
            m.login(username, password);
553
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
554

    
555
            // upload a document which gives read access to the online document
556
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Another insert",
557
                                         null, null,
558
                                         "ecogrid://knb/" + onlineDocid + ".1",
559
                                         null, getAccessBlock(anotheruser, true,
560
                                         false, false, true, false),
561
                                         null, null, null, null);
562
            newdocid = generateDocid();
563
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
564
            m.logout();
565

    
566
            // login as another user
567
            m.login(anotheruser, anotherpassword);
568

    
569
            // try to read the online data
570
            readDocid(onlineDocid + ".1", FAILURE, true);
571

    
572
            // try to upload another data with updated id
573
            uploadDocid(onlineDocid + ".2",
574
                          onlinetestdatafile2, FAILURE, true);
575

    
576
            // try to set the permissions for the uploaded document
577
            // the docid given is for the online document
578
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
579
                                         null, null,
580
                                         "ecogrid://knb/" + onlineDocid + ".1",
581
                                         null, getAccessBlock(anotheruser, true,
582
                                         false, false, false, true),
583
                                         null, null, null, null);
584
            newdocid = generateDocid();
585
            // ERRRRRRRRRRRRRRRR
586
            // User does not have permission to update of access rules for data
587
            // insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
588

    
589
            // delete the document
590
            deleteDocid(onlineDocid + ".1", FAILURE, true);
591
            m.logout();
592

    
593
            /////////Case 4/////////////////////
594
            // upload an online document all
595
            onlineDocid = generateDocid();
596
            m.login(username, password);
597
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
598

    
599
            // upload a document which gives read access to the online document
600
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
601
                                         null, null,
602
                                         "ecogrid://knb/" + onlineDocid + ".1",
603
                                         null, getAccessBlock(anotheruser, true,
604
                                         false, false, false, true),
605
                                         null, null, null, null);
606
            newdocid = generateDocid();
607
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
608
            m.logout();
609

    
610
            // login as another user
611
            m.login(anotheruser, anotherpassword);
612

    
613
            // try to read the online data
614
            readDocid(onlineDocid + ".1", SUCCESS, false);
615

    
616
            // try to upload another data with updated id
617
            uploadDocid(onlineDocid + ".2",
618
                        onlinetestdatafile2, SUCCESS, false);
619

    
620
            // try to set the permissions for the uploaded document
621
            // the docid given is for the online document
622
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
623
                                         null, null,
624
                                         "ecogrid://knb/" + onlineDocid + ".1",
625
                                         null, getAccessBlock(anotheruser, true,
626
                                         true, false, false, false),
627
                                         null, null, null, null);
628
            newdocid = generateDocid();
629
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
630

    
631
            m.logout();
632
            // delete the document
633
            deleteDocid(onlineDocid + ".1", FAILURE, false);
634

    
635
            m.logout();
636

    
637
        }
638
        catch (MetacatAuthException mae) {
639
            fail("Authorization failed:\n" + mae.getMessage());
640
        }
641
        catch (MetacatInaccessibleException mie) {
642
            fail("Metacat Inaccessible:\n" + mie.getMessage());
643
        }
644
        catch (Exception e) {
645
            fail("General exception:\n" + e.getMessage());
646
        }
647
    }
648

    
649

    
650

    
651
    /** *********
652
     * Checking the following cases:
653
     * when only online data is uploaded by a user with the following different
654
     * access controls specified in addiotnal metadata in another document
655
     *   1.read
656
     *   2.write
657
     *   3.change permission
658
     *   4.all
659
     * And another user tries to do the following:
660
     * -> tries to read it
661
     * -> tries to update it
662
     * -> tries to set permissions on it
663
     * -> tries to delete it
664
     */
665
    public void onlineDataCasesTest_4() {
666
        try {
667

    
668
            /////////Case 1./////////////////////
669
            // upload an online document - read only
670
            onlineDocid = generateDocid();
671
            m.login(username, password);
672
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
673

    
674
            // upload a document which gives read access to the online document
675
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
676
                                         null, null,
677
                                         "ecogrid://knb/" + onlineDocid + ".1",
678
                                         null, null, null, null,
679
                                         getAccessBlock(anotheruser, true,
680
                                         true, false, false, false), null);
681
            newdocid = generateDocid();
682
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
683
            m.logout();
684

    
685
            // login as another user
686
            m.login(anotheruser, anotherpassword);
687

    
688
            // try to read the online data
689
            readDocid(onlineDocid + ".1", SUCCESS, false);
690

    
691
            // try to upload another data with updated id
692
            uploadDocid(onlineDocid + ".2",
693
                        onlinetestdatafile2, FAILURE, true);
694

    
695
            // try to set the permissions for the uploaded document
696
            // the docid given is for the online document
697
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
698
                                         null, null,
699
                                         "ecogrid://knb/" + onlineDocid + ".1",
700
                                         null, null, null, null,
701
                                         getAccessBlock(anotheruser, true,
702
                                         false, false, false, true), null);
703
            newdocid = generateDocid();
704
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
705

    
706
            // delete the document
707
            deleteDocid(onlineDocid + ".1", FAILURE, true);
708
            m.logout();
709

    
710
            /////////Case 2/////////////////////
711
            // upload an online document - write only
712
            onlineDocid = generateDocid();
713
            m.login(username, password);
714
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
715

    
716
            // upload a document which gives read access to the online document
717
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
718
                                         null, null,
719
                                         "ecogrid://knb/" + onlineDocid + ".1",
720
                                         null, null, null, null,
721
                                         getAccessBlock(anotheruser, true,
722
                                         false, true, false, false), null);
723
            newdocid = generateDocid();
724
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
725
            m.logout();
726

    
727
            // login as another user
728
            m.login(anotheruser, anotherpassword);
729

    
730
            // try to read the online data
731
            readDocid(onlineDocid + ".1", FAILURE, true);
732

    
733
            // try to upload another data with updated id
734
            uploadDocid(onlineDocid + ".2",
735
                        onlinetestdatafile2, SUCCESS, false);
736

    
737
            // try to set the permissions for the uploaded document
738
            // the docid given is for the online document
739
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
740
                                         null, null,
741
                                         "ecogrid://knb/" + onlineDocid + ".1",
742
                                         null, null, null, null,
743
                                         getAccessBlock(anotheruser, true,
744
                                         false, false, false, true), null);
745
            newdocid = generateDocid();
746
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
747

    
748
            // delete the document
749
            deleteDocid(onlineDocid + ".2", FAILURE, true);
750
            m.logout();
751

    
752
            /////////Case 3/////////////////////
753
            // upload an online document - change permission only
754
            onlineDocid = generateDocid();
755
            m.login(username, password);
756
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
757

    
758
            // upload a document which gives read access to the online document
759
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
760
                                         null, null,
761
                                         "ecogrid://knb/" + onlineDocid + ".1",
762
                                         null, null, null, null,
763
                                         getAccessBlock(anotheruser, true,
764
                                         false, false, true, false), null);
765
            newdocid = generateDocid();
766
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
767
            m.logout();
768

    
769
            // login as another user
770
            m.login(anotheruser, anotherpassword);
771

    
772
            // try to read the online data
773
            readDocid(onlineDocid + ".1", FAILURE, true);
774

    
775
            // try to upload another data with updated id
776
            uploadDocid(onlineDocid + ".2",
777
                        onlinetestdatafile2, FAILURE, true);
778

    
779
            // try to set the permissions for the uploaded document
780
            // the docid given is for the online document
781
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
782
                                         null, null,
783
                                         "ecogrid://knb/" + onlineDocid + ".1",
784
                                         null, null, null, null,
785
                                         getAccessBlock(anotheruser, true,
786
                                         false, false, false, true), null);
787
            newdocid = generateDocid();
788
            // ERRRRRRRRRRRRRRRR
789
            // User does not have permission to update of access rules for data
790
            // insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
791

    
792
            // delete the document
793
            deleteDocid(onlineDocid + ".1", FAILURE, true);
794
            m.logout();
795

    
796
            /////////Case 4/////////////////////
797
            // upload an online document all
798
            onlineDocid = generateDocid();
799
            m.login(username, password);
800
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
801

    
802
            // upload a document which gives read access to the online document
803
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
804
                                         null, null,
805
                                         "ecogrid://knb/" + onlineDocid + ".1",
806
                                         null, null, null, null,
807
                                         getAccessBlock(anotheruser, true,
808
                                         false, false, false, true), null);
809
            newdocid = generateDocid();
810
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
811
            m.logout();
812

    
813
            // login as another user
814
            m.login(anotheruser, anotherpassword);
815

    
816
            // try to read the online data
817
            readDocid(onlineDocid + ".1", SUCCESS, false);
818

    
819
            // try to upload another data with updated id
820
            uploadDocid(onlineDocid + ".2",
821
                        onlinetestdatafile2, SUCCESS, false);
822

    
823
            // try to set the permissions for the uploaded document
824
            // the docid given is for the online document
825
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
826
                                         null, null,
827
                                         "ecogrid://knb/" + onlineDocid + ".1",
828
                                         null, null, null, null,
829
                                         getAccessBlock(anotheruser, true,
830
                                         true, false, false, false), null);
831
            newdocid = generateDocid();
832
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
833

    
834
            m.logout();
835
            // delete the document
836
            deleteDocid(onlineDocid + ".1", FAILURE, false);
837

    
838
            m.logout();
839

    
840
        }
841
        catch (MetacatAuthException mae) {
842
            fail("Authorization failed:\n" + mae.getMessage());
843
        }
844
        catch (MetacatInaccessibleException mie) {
845
            fail("Metacat Inaccessible:\n" + mie.getMessage());
846
        }
847
        catch (Exception e) {
848
            fail("General exception:\n" + e.getMessage());
849
        }
850
    }
851

    
852
    /** *********
853
     * Checking the following cases:
854
     * -> when online data with document refering to it is uploaded with
855
     *    rules in additional metadata for an wrong entity id which
856
     *    doesnt exist
857
     * -> when online data with document refering to it is uploaded with
858
     *    rules in additional metadata for an entity which doesnt
859
     *    exist - wrong url
860
     */
861
    public void onlineDataCasesTest_5() {
862
        try {
863

    
864
            // upload online data
865
            onlineDocid = generateDocid();
866
            m.login(username, password);
867

    
868
            /////////Case 1
869
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert", null, null,
870
                                         "ecogrid://knb/" + onlineDocid + ".1",
871
                                         null, null, null, null, null,
872
                                         getAccessBlock(anotheruser, true,
873
                                         true, false, false, false));
874
            newdocid = generateDocid();
875
            insertDocid(newdocid + ".1", testdocument, FAILURE, false);
876

    
877
            /////////Case 2
878
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert", null, null,
879
                                         "ecogrid://knb/" + onlineDocid + ".1",
880
                                         null, null, null, null,
881
                                         getAccessBlock(anotheruser, true,
882
                                         true, false, false, false), null);
883
            newdocid = generateDocid();
884

    
885
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
886
            m.logout();
887

    
888
        }
889
        catch (MetacatAuthException mae) {
890
            fail("Authorization failed:\n" + mae.getMessage());
891
        }
892
        catch (MetacatInaccessibleException mie) {
893
            fail("Metacat Inaccessible:\n" + mie.getMessage());
894
        }
895
        catch (Exception e) {
896
            fail("General exception:\n" + e.getMessage());
897
        }
898
    }
899

    
900

    
901
    /** *********
902
     * Checking the following cases:
903
     * -> when a document is added with no online data - it is updated (has
904
     *    access)  - then data is added - and a url to it is added and docid
905
     *    is updated - then the access is updated in document
906
     *    does it result in rules being applied on the data
907
     *    (Andrea Chadden was having problem is a similar case)
908
     * -> when online data with document refering to it is uploaded with read
909
     *    access for document and no access for docid and vice versa
910
     */
911
    public void onlineDataCasesTest_6() {
912
        try {
913

    
914
            // insert a document
915
            m.login(username, password);
916
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
917
                                         null, null, null,
918
                                         null, getAccessBlock(anotheruser, true,
919
                                         true, false, false, false), null, null,
920
                                         null, null);
921
            newdocid = generateDocid();
922
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
923
            m.logout();
924

    
925

    
926
            // update document
927
            m.login(username, password);
928
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing update",
929
                                         null, null, null,
930
                                         null, getAccessBlock(anotheruser, true,
931
                                         true, false, false, false), null, null,
932
                                         null, null);
933
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
934
            m.logout();
935

    
936

    
937
            // upload data and update the document
938
            onlineDocid = generateDocid();
939
            m.login(username, password);
940
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
941
            m.logout();
942

    
943
            // try to read the online data
944
            m.login(anotheruser, anotherpassword);
945
            readDocid(onlineDocid + ".1", FAILURE, true);
946
            m.logout();
947

    
948
            // upload data and update the document
949
            m.login(username, password);
950
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing update",
951
                                         null, null,
952
                                         "ecogrid://knb/" + onlineDocid + ".1",
953
                                         null, getAccessBlock(anotheruser, true,
954
                                         true, false, false, false), null, null,
955
                                         null, null);
956
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
957
            m.logout();
958

    
959
            // set read for document - no read for data
960
            m.login(username, password);
961
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
962
                                         null, null,
963
                                         "ecogrid://knb/" + onlineDocid + ".1",
964
                                         null, getAccessBlock(anotheruser, true,
965
                                         true, false, false, false), null, null,
966
                                         getAccessBlock(anotheruser, false,
967
                                         false, false, false, true), null);
968
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
969
            m.logout();
970

    
971
            // try to read the online data
972
            m.login(anotheruser, anotherpassword);
973
            readDocid(newdocid + ".4", SUCCESS, false);
974
            readDocid(onlineDocid + ".1", FAILURE, true);
975
            m.logout();
976

    
977

    
978
            // set read for document - no read for data
979
            m.login(username, password);
980
            testdocument = getTestEmlDoc("Doing update", null, null,
981
                                         "ecogrid://knb/" + onlineDocid + ".1",
982
                                         null, getAccessBlock(anotheruser, false,
983
                                         false, false, false, true), null, null,
984
                                         getAccessBlock(anotheruser, true,
985
                                         true, false, false, false), null);
986
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
987
            m.logout();
988

    
989
            // try to read the online data
990
            m.login(anotheruser, anotherpassword);
991
            readDocid(newdocid + ".5", FAILURE, true);
992
            readDocid(onlineDocid + ".1", SUCCESS, false);
993
            m.logout();
994
        }
995
        catch (MetacatAuthException mae) {
996
            fail("Authorization failed:\n" + mae.getMessage());
997
        }
998
        catch (MetacatInaccessibleException mie) {
999
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1000
        }
1001
        catch (Exception e) {
1002
            fail("General exception:\n" + e.getMessage());
1003
        }
1004
    }
1005

    
1006

    
1007
    /**
1008
     * Insert a document into metacat. The expected result is passed as result
1009
     */
1010

    
1011
    private String insertDocid(String docid, String docText, boolean result,
1012
                               boolean expectKarmaException) {
1013
        String response = null;
1014
        try {
1015
            response = m.insert(docid,
1016
                                new StringReader(testdocument), null);
1017
            if (result) {
1018
                assertTrue( (response.indexOf("<success>") != -1));
1019
                assertTrue(response.indexOf(docid) != -1);
1020
            }
1021
            else {
1022
                assertTrue( (response.indexOf("<success>") == -1));
1023
            }
1024
            System.err.println(response);
1025
        }
1026
        catch (MetacatInaccessibleException mie) {
1027
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1028
        }
1029
        catch (InsufficientKarmaException ike) {
1030
            if (!expectKarmaException) {
1031
                fail("Insufficient karma:\n" + ike.getMessage());
1032
            }
1033
        }
1034
        catch (MetacatException me) {
1035
            if (result) {
1036
               fail("Metacat Error:\n" + me.getMessage());
1037
           }
1038
           else {
1039
               System.err.println("Metacat Error: " + me.getMessage());
1040
           }
1041
        }
1042
        catch (Exception e) {
1043
            fail("General exception:\n" + e.getMessage());
1044
        }
1045
        return response;
1046
    }
1047

    
1048
    /**
1049
     * Insert a document into metacat. The expected result is passed as result
1050
     */
1051

    
1052
    private String uploadDocid(String docid, String filePath, boolean result,
1053
                               boolean expectedKarmaException) {
1054
        String response = null;
1055
        try {
1056
            response = m.upload(docid, new File(filePath));
1057
            if (result) {
1058
                assertTrue( (response.indexOf("<success>") != -1));
1059
                assertTrue(response.indexOf(docid) != -1);
1060
            }
1061
            else {
1062
                assertTrue( (response.indexOf("<success>") == -1));
1063
            }
1064
            System.err.println("respose from metacat: " + response);
1065
        }
1066
        catch (MetacatInaccessibleException mie) {
1067
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1068
        }
1069
        catch (InsufficientKarmaException ike) {
1070
            if (!expectedKarmaException) {
1071
                fail("Insufficient karma:\n" + ike.getMessage());
1072
            }
1073
        }
1074
        catch (MetacatException me) {
1075
            if (result) {
1076
                fail("Metacat Error:\n" + me.getMessage());
1077
            }
1078
            else {
1079
                System.err.println("Metacat Error: " + me.getMessage());
1080
            }
1081
        }
1082
        catch (Exception e) {
1083
            fail("General exception:\n" + e.getMessage());
1084
        }
1085
        return response;
1086
    }
1087

    
1088
    /**
1089
     * Update a document in metacat. The expected result is passed as result
1090
     */
1091
    private String updateDocid(String docid, String docText, boolean result,
1092
                               boolean expectedKarmaFailure) {
1093
        String response = null;
1094
        try {
1095
            response = m.update(docid,
1096
                                new StringReader(testdocument), null);
1097

    
1098
            if (result) {
1099
                assertTrue( (response.indexOf("<success>") != -1));
1100
                assertTrue(response.indexOf(docid) != -1);
1101
            }
1102
            else {
1103
                assertTrue( (response.indexOf("<success>") == -1));
1104
            }
1105
            System.err.println(response);
1106
        }
1107
        catch (MetacatInaccessibleException mie) {
1108
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1109
        }
1110
        catch (InsufficientKarmaException ike) {
1111
            if (!expectedKarmaFailure) {
1112
                fail("Insufficient karma:\n" + ike.getMessage());
1113
            }
1114
        }
1115
        catch (MetacatException me) {
1116
            if (! (expectedKarmaFailure &&
1117
                   (me.getMessage().indexOf(
1118
                "User try to update a access module which it doesn't have \"ALL\" permission") !=
1119
                    -1))) {
1120
                fail("Metacat Error:\n" + me.getMessage());
1121
            }
1122
        }
1123
        catch (Exception e) {
1124
            fail("General exception:\n" + e.getMessage());
1125
        }
1126

    
1127
        return response;
1128
    }
1129

    
1130
    /**
1131
     * Delete a document into metacat. The expected result is passed as result
1132
     */
1133
    private void deleteDocid(String docid, boolean result,
1134
                             boolean expectedKarmaFailure) {
1135
        try {
1136
            String response = m.delete(docid);
1137
            if (result) {
1138
                assertTrue(response.indexOf("<success>") != -1);
1139
            }
1140
            else {
1141
                assertTrue(response.indexOf("<success>") == -1);
1142
            }
1143
            System.err.println(response);
1144
        }
1145
        catch (MetacatInaccessibleException mie) {
1146
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1147
        }
1148
        catch (InsufficientKarmaException ike) {
1149
            if (!expectedKarmaFailure) {
1150
                    fail("Insufficient karma:\n" + ike.getMessage());
1151
                }
1152

    
1153
            }
1154
        catch (MetacatException me) {
1155
            if (result) {
1156
                fail("Metacat Error:\n" + me.getMessage());
1157
            }
1158
            else {
1159
                System.err.println("Metacat Error:\n" + me.getMessage());
1160
            }
1161
        }
1162
        catch (Exception e) {
1163
            fail("General exception:\n" + e.getMessage());
1164
        }
1165
    }
1166

    
1167
    /**
1168
     * Read a document from metacat. The expected result is passed as result
1169
     */
1170
    private void readDocid(String docid, boolean result,
1171
                           boolean expextedKarmaFailure) {
1172
        try {
1173
            Reader r = m.read(docid);
1174
            String response = IOUtil.getAsString(r, true);
1175

    
1176
            if (!result) {
1177
                assertTrue(response.indexOf("<success>") == -1);
1178
            }
1179
            // System.err.println(response);
1180
        }
1181
        catch (MetacatInaccessibleException mie) {
1182
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1183
        }
1184
        catch (InsufficientKarmaException ike) {
1185
            if (!expextedKarmaFailure) {
1186
                fail("Insufficient karma:\n" + ike.getMessage());
1187
            }
1188
        }
1189
        catch (MetacatException me) {
1190
            if (result) {
1191
                fail("Metacat Error:\n" + me.getMessage());
1192
            }
1193
            else {
1194
                System.err.println("Metacat Error:\n" + me.getMessage());
1195
            }
1196
        }
1197
        catch (Exception e) {
1198
            fail("General exception:\n" + e.getMessage());
1199
        }
1200
    }
1201

    
1202
    /**
1203
     * Read a document from metacat and check if it is equal to a given string.
1204
     * The expected result is passed as result
1205
     */
1206

    
1207
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
1208
                                         boolean result,
1209
                                         boolean expextedKarmaFailure) {
1210
        try {
1211
            Reader r = m.read(docid);
1212
            String doc = IOUtil.getAsString(r, true);
1213
            if (result) {
1214

    
1215
                if (!testDoc.equals(doc)) {
1216
                    System.out.println("doc    :" + doc);
1217
                    System.out.println("testDoc:" + testDoc);
1218
                }
1219

    
1220
                assertTrue(testDoc.equals(doc));
1221
            }
1222
            else {
1223
                assertTrue(doc.indexOf("<error>") != -1);
1224
            }
1225
        }
1226
        catch (MetacatInaccessibleException mie) {
1227
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1228
        }
1229
        catch (InsufficientKarmaException ike) {
1230
            if (!expextedKarmaFailure) {
1231
                fail("Insufficient karma:\n" + ike.getMessage());
1232
            }
1233
        }
1234
        catch (MetacatException me) {
1235
            fail("Metacat Error:\n" + me.getMessage());
1236
        }
1237
        catch (Exception e) {
1238
            fail("General exception:\n" + e.getMessage());
1239
        }
1240

    
1241
    }
1242

    
1243
    /**
1244
     * Create a hopefully unique docid for testing insert and update. Does
1245
     * not include the 'revision' part of the id.
1246
     *
1247
     * @return a String docid based on the current date and time
1248
     */
1249
    private String generateDocid() {
1250
        StringBuffer docid = new StringBuffer(prefix);
1251
        docid.append(".");
1252

    
1253
        // Create a calendar to get the date formatted properly
1254
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
1255
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
1256
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
1257
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
1258
                       2 * 60 * 60 * 1000);
1259
        Calendar calendar = new GregorianCalendar(pdt);
1260
        Date trialTime = new Date();
1261
        calendar.setTime(trialTime);
1262
        docid.append(calendar.get(Calendar.YEAR));
1263
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
1264
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
1265
        docid.append(calendar.get(Calendar.MINUTE));
1266
        docid.append(calendar.get(Calendar.SECOND));
1267

    
1268
        return docid.toString();
1269
    }
1270
}
(8-8/13)