1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.query.internal

File SecureQueryExecutorManagerTest.java

 

Code metrics

0
20
5
1
122
72
6
0.3
4
5
1.2

Classes

Class Line # Actions
SecureQueryExecutorManagerTest 47 20 0% 6 1
0.9696%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.query.internal;
21   
22    import org.junit.Before;
23    import org.junit.Rule;
24    import org.junit.Test;
25    import org.mockito.invocation.InvocationOnMock;
26    import org.mockito.stubbing.Answer;
27    import org.xwiki.query.Query;
28    import org.xwiki.query.QueryException;
29    import org.xwiki.query.QueryExecutorManager;
30    import org.xwiki.security.authorization.ContextualAuthorizationManager;
31    import org.xwiki.security.authorization.Right;
32    import org.xwiki.test.mockito.MockitoComponentMockingRule;
33   
34    import static org.junit.Assert.assertTrue;
35   
36    import static org.junit.Assert.assertFalse;
37    import static org.junit.Assert.assertEquals;
38    import static org.junit.Assert.fail;
39    import static org.mockito.Mockito.mock;
40    import static org.mockito.Mockito.when;
41   
42    /**
43    * Tests for {@link SecureQueryExecutorManager}
44    *
45    * @version $Id: df7d111fa02a31959de79ea3442a4dd05a2eb152 $
46    */
 
47    public class SecureQueryExecutorManagerTest
48    {
49    @Rule
50    public MockitoComponentMockingRule<QueryExecutorManager> mocker =
51    new MockitoComponentMockingRule<QueryExecutorManager>(SecureQueryExecutorManager.class);
52   
53    private ContextualAuthorizationManager authorization;
54   
55    private boolean hasProgrammingRight;
56   
57    /**
58    * The component under test.
59    */
60    private QueryExecutorManager executor;
61   
 
62  3 toggle @Before
63    public void before() throws Exception
64    {
65  3 this.executor = this.mocker.getComponentUnderTest();
66  3 this.authorization = this.mocker.getInstance(ContextualAuthorizationManager.class);
67   
68  3 when(this.authorization.hasAccess(Right.PROGRAM)).then(new Answer<Boolean>()
69    {
 
70  2 toggle @Override
71    public Boolean answer(InvocationOnMock invocation) throws Throwable
72    {
73  2 return hasProgrammingRight;
74    }
75    });
76   
77  3 this.hasProgrammingRight = true;
78    }
79   
80    // Tests
81   
 
82  1 toggle @Test
83    public void executeNotSecureQueryWithoutProgrammingRight()
84    {
85  1 this.hasProgrammingRight = false;
86   
87    // Create a Query not implementing SecureQuery
88  1 Query query = mock(Query.class);
89   
90  1 try {
91  1 this.executor.execute(query);
92  0 fail("Should have thrown an exception here");
93    } catch (QueryException expected) {
94  1 assertEquals("Unsecure query require programming right. Query statement = [null]",
95    expected.getMessage());
96    }
97    }
98   
 
99  1 toggle @Test
100    public void executeNotSecureQueryWithProgrammingRight() throws QueryException
101    {
102  1 this.hasProgrammingRight = true;
103   
104  1 Query query = mock(Query.class);
105   
106  1 this.executor.execute(query);
107    }
108   
 
109  1 toggle @Test
110    public void executeSecureQueryWithoutCheckCurrentAuthor() throws QueryException
111    {
112  1 DefaultQuery query = new DefaultQuery("statement", "language", this.executor);
113   
114  1 assertFalse(query.isCurrentAuthorChecked());
115  1 ;
116   
117  1 this.executor.execute(query);
118   
119  1 assertTrue(query.isCurrentAuthorChecked());
120  1 ;
121    }
122    }