/
SetACLProcessorTest.java
266 lines (209 loc) · 12.1 KB
/
SetACLProcessorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
package org.apache.james.imap.processor;
import org.apache.james.imap.api.ImapCommand;
import org.apache.james.imap.api.ImapConstants;
import org.apache.james.imap.api.ImapSessionState;
import org.apache.james.imap.api.ImapSessionUtils;
import org.apache.james.imap.api.message.response.StatusResponse;
import org.apache.james.imap.api.process.ImapProcessor;
import org.apache.james.imap.api.process.ImapProcessor.Responder;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.message.request.SetACLRequest;
import org.apache.james.imap.message.response.UnpooledStatusResponseFactory;
import org.apache.james.mailbox.MailboxManager;
import org.apache.james.mailbox.MailboxSession;
import org.apache.james.mailbox.MailboxSession.User;
import org.apache.james.mailbox.MessageManager;
import org.apache.james.mailbox.MessageManager.MetaData;
import org.apache.james.mailbox.MessageManager.MetaData.FetchGroup;
import org.apache.james.mailbox.exception.MailboxException;
import org.apache.james.mailbox.exception.MailboxNotFoundException;
import org.apache.james.mailbox.model.MailboxACL;
import org.apache.james.mailbox.model.MailboxACL.EditMode;
import org.apache.james.mailbox.model.MailboxACL.MailboxACLEntryKey;
import org.apache.james.mailbox.model.MailboxACL.MailboxACLRights;
import org.apache.james.mailbox.model.MailboxPath;
import org.apache.james.mailbox.model.SimpleMailboxACL;
import org.apache.james.mailbox.model.SimpleMailboxACL.Rfc4314Rights;
import org.apache.james.mailbox.model.SimpleMailboxACL.SimpleMailboxACLEntryKey;
import org.apache.james.metrics.api.NoopMetricFactory;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* SetACLProcessor Test.
*
* @author Peter Palaga
*/
@RunWith(JMock.class)
public class SetACLProcessorTest {
private static final String MAILBOX_NAME = ImapConstants.INBOX_NAME;
private static final String USER_1 = "user1";
private static final String SET_RIGHTS = "aw";
private static final String UNSUPPORTED_RIGHT = "W";
ImapSession imapSessionStub;
MailboxManager mailboxManagerStub;
MailboxSession mailboxSessionStub;
MessageManager messageManagerStub;
MetaData metaDataStub;
Mockery mockery = new JUnit4Mockery();
SetACLRequest replaceACLRequest;
UnpooledStatusResponseFactory statusResponseFactory;
SetACLProcessor subject;
User user1Stub;
MailboxACLEntryKey user1Key;
MailboxACLRights setRights;
MailboxPath path;
private Expectations prepareRightsExpectations() throws MailboxException {
return new Expectations() {
{
allowing(imapSessionStub).getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY);
will(returnValue(mailboxSessionStub));
allowing(imapSessionStub).getState();
will(returnValue(ImapSessionState.AUTHENTICATED));
allowing(mailboxSessionStub).getUser();
will(returnValue(user1Stub));
allowing(user1Stub).getUserName();
will(returnValue(USER_1));
allowing(mailboxManagerStub).startProcessingRequest(with(same(mailboxSessionStub)));
allowing(mailboxManagerStub).endProcessingRequest(with(same(mailboxSessionStub)));
allowing(messageManagerStub).getMetaData(with(any(Boolean.class)), with(same(mailboxSessionStub)), with(any(FetchGroup.class)));
will(returnValue(metaDataStub));
}
};
}
@Before
public void setUp() throws Exception {
path = new MailboxPath("#private", USER_1, MAILBOX_NAME);
statusResponseFactory = new UnpooledStatusResponseFactory();
mailboxManagerStub = mockery.mock(MailboxManager.class);
subject = new SetACLProcessor(mockery.mock(ImapProcessor.class), mailboxManagerStub, statusResponseFactory, new NoopMetricFactory());
imapSessionStub = mockery.mock(ImapSession.class);
mailboxSessionStub = mockery.mock(MailboxSession.class);
user1Stub = mockery.mock(User.class);
messageManagerStub = mockery.mock(MessageManager.class);
metaDataStub = mockery.mock(MetaData.class);
replaceACLRequest = new SetACLRequest("TAG", ImapCommand.anyStateCommand("Name"), MAILBOX_NAME, USER_1, SET_RIGHTS);
user1Key = new SimpleMailboxACLEntryKey(USER_1);
setRights = new Rfc4314Rights(SET_RIGHTS);
}
@Test
public void testUnsupportedRight() throws Exception {
Expectations expectations = prepareRightsExpectations();
expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(Rfc4314Rights.l_Lookup_RIGHT)), expectations.with(Expectations.same(mailboxSessionStub)));
expectations.will(Expectations.returnValue(false));
expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class)));
expectations.will(Expectations.returnValue(messageManagerStub));
mockery.checking(expectations);
final Responder responderMock = mockery.mock(Responder.class);
mockery.checking(new Expectations() {
{
oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.BAD)));
}
});
subject.doProcess(new SetACLRequest("TAG", ImapCommand.anyStateCommand("Name"), MAILBOX_NAME, USER_1, UNSUPPORTED_RIGHT), responderMock, imapSessionStub);
}
@Test
public void testNoListRight() throws Exception {
Expectations expectations = prepareRightsExpectations();
expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(Rfc4314Rights.l_Lookup_RIGHT)), expectations.with(Expectations.same(mailboxSessionStub)));
expectations.will(Expectations.returnValue(false));
expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class)));
expectations.will(Expectations.returnValue(messageManagerStub));
mockery.checking(expectations);
final Responder responderMock = mockery.mock(Responder.class);
mockery.checking(new Expectations() {
{
oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO)));
}
});
subject.doProcess(replaceACLRequest, responderMock, imapSessionStub);
}
@Test
public void testNoAdminRight() throws Exception {
Expectations expectations = prepareRightsExpectations();
expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(Rfc4314Rights.l_Lookup_RIGHT)), expectations.with(Expectations.same(mailboxSessionStub)));
expectations.will(Expectations.returnValue(true));
expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(Rfc4314Rights.a_Administer_RIGHT)), expectations.with(Expectations.same(mailboxSessionStub)));
expectations.will(Expectations.returnValue(false));
expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class)));
expectations.will(Expectations.returnValue(messageManagerStub));
mockery.checking(expectations);
final Responder responderMock = mockery.mock(Responder.class);
mockery.checking(new Expectations() {
{
oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO)));
}
});
subject.doProcess(replaceACLRequest, responderMock, imapSessionStub);
}
@Test
public void testInexistentMailboxName() throws Exception {
Expectations expectations = prepareRightsExpectations();
expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class)));
expectations.will(Expectations.throwException(new MailboxNotFoundException(MAILBOX_NAME)));
mockery.checking(expectations);
final Responder responderMock = mockery.mock(Responder.class);
mockery.checking(new Expectations() {
{
oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.NO)));
}
});
subject.doProcess(replaceACLRequest, responderMock, imapSessionStub);
}
@Test
public void testAddRights() throws Exception {
testOp("+", EditMode.ADD);
}
private void testOp(String prefix, EditMode editMode) throws MailboxException {
final MailboxACL acl = SimpleMailboxACL.OWNER_FULL_ACL;
Expectations expectations = prepareRightsExpectations();
expectations.allowing(mailboxManagerStub).getMailbox(expectations.with(Expectations.any(MailboxPath.class)), expectations.with(Expectations.any(MailboxSession.class)));
expectations.will(Expectations.returnValue(messageManagerStub));
expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(Rfc4314Rights.l_Lookup_RIGHT)), expectations.with(Expectations.same(mailboxSessionStub)));
expectations.will(Expectations.returnValue(true));
expectations.allowing(mailboxManagerStub).hasRight(expectations.with(path), expectations.with(Expectations.equal(Rfc4314Rights.a_Administer_RIGHT)), expectations.with(Expectations.same(mailboxSessionStub)));
expectations.will(Expectations.returnValue(true));
expectations.allowing(mailboxManagerStub).setRights(expectations.with(path), expectations.with(Expectations.equal(new SimpleMailboxACL.SimpleMailboxACLCommand(user1Key, editMode, setRights))), expectations.with(mailboxSessionStub));
expectations.allowing(metaDataStub).getACL();
expectations.will(Expectations.returnValue(acl));
mockery.checking(expectations);
final Responder responderMock = mockery.mock(Responder.class);
mockery.checking(new Expectations() {
{
oneOf(responderMock).respond(with(new StatusResponseTypeMatcher(StatusResponse.Type.OK)));
}
});
SetACLRequest r = new SetACLRequest("TAG", ImapCommand.anyStateCommand("Name"), MAILBOX_NAME, USER_1, prefix + SET_RIGHTS);
subject.doProcess(r, responderMock, imapSessionStub);
}
@Test
public void testRemoveRights() throws Exception {
testOp("-", EditMode.REMOVE);
}
@Test
public void testReplaceRights() throws Exception {
testOp("", EditMode.REPLACE);
}
}