Skip to content

Commit 5f6dc52

Browse files
committed
added tests
1 parent f8c77ff commit 5f6dc52

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright (C) 2026 Dominik Schadow, dominikschadow@gmail.com
3+
*
4+
* This file is part of the Java Security project.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package de.dominikschadow.javasecurity.contacts;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.context.SpringBootTest;
23+
import org.springframework.security.access.AccessDeniedException;
24+
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
25+
import org.springframework.security.test.context.support.WithMockUser;
26+
27+
import java.util.List;
28+
29+
import static org.junit.jupiter.api.Assertions.*;
30+
31+
/**
32+
* Tests for {@link ContactService} to verify Spring Security method-level security annotations.
33+
*
34+
* @author Dominik Schadow
35+
*/
36+
@SpringBootTest
37+
class ContactServiceTest {
38+
@Autowired
39+
private ContactService contactService;
40+
41+
@Test
42+
void getContact_withoutAuthentication_throwsException() {
43+
assertThrows(AuthenticationCredentialsNotFoundException.class, () -> contactService.getContact(1));
44+
}
45+
46+
@Test
47+
@WithMockUser(username = "userA", roles = "USER")
48+
void getContact_asUserA_withOwnContact_returnsContact() {
49+
Contact contact = contactService.getContact(1);
50+
51+
assertNotNull(contact);
52+
assertEquals("userA", contact.getUsername());
53+
assertEquals("Zaphod", contact.getFirstname());
54+
assertEquals("Beeblebrox", contact.getLastname());
55+
}
56+
57+
@Test
58+
@WithMockUser(username = "userA", roles = "USER")
59+
void getContact_asUserA_withOtherUsersContact_throwsAccessDenied() {
60+
// Contact with id 3 belongs to userB
61+
assertThrows(AccessDeniedException.class, () -> contactService.getContact(3));
62+
}
63+
64+
@Test
65+
@WithMockUser(username = "userB", roles = "USER")
66+
void getContact_asUserB_withOwnContact_returnsContact() {
67+
Contact contact = contactService.getContact(3);
68+
69+
assertNotNull(contact);
70+
assertEquals("userB", contact.getUsername());
71+
assertEquals("Arthur", contact.getFirstname());
72+
assertEquals("Dent", contact.getLastname());
73+
}
74+
75+
@Test
76+
@WithMockUser(username = "userB", roles = "USER")
77+
void getContact_asUserB_withOtherUsersContact_throwsAccessDenied() {
78+
// Contact with id 1 belongs to userA
79+
assertThrows(AccessDeniedException.class, () -> contactService.getContact(1));
80+
}
81+
82+
@Test
83+
@WithMockUser(username = "userA", roles = "ADMIN")
84+
void getContact_withWrongRole_throwsAccessDenied() {
85+
assertThrows(AccessDeniedException.class, () -> contactService.getContact(1));
86+
}
87+
88+
@Test
89+
void getContacts_withoutAuthentication_throwsException() {
90+
assertThrows(AuthenticationCredentialsNotFoundException.class, () -> contactService.getContacts());
91+
}
92+
93+
@Test
94+
@WithMockUser(username = "userA", roles = "USER")
95+
void getContacts_asUserA_returnsOnlyUserAContacts() {
96+
List<Contact> contacts = contactService.getContacts();
97+
98+
assertNotNull(contacts);
99+
assertEquals(2, contacts.size());
100+
assertTrue(contacts.stream().allMatch(c -> "userA".equals(c.getUsername())));
101+
assertTrue(contacts.stream().anyMatch(c -> "Zaphod".equals(c.getFirstname())));
102+
assertTrue(contacts.stream().anyMatch(c -> "Ford".equals(c.getFirstname())));
103+
}
104+
105+
@Test
106+
@WithMockUser(username = "userB", roles = "USER")
107+
void getContacts_asUserB_returnsOnlyUserBContacts() {
108+
List<Contact> contacts = contactService.getContacts();
109+
110+
assertNotNull(contacts);
111+
assertEquals(2, contacts.size());
112+
assertTrue(contacts.stream().allMatch(c -> "userB".equals(c.getUsername())));
113+
assertTrue(contacts.stream().anyMatch(c -> "Arthur".equals(c.getFirstname())));
114+
assertTrue(contacts.stream().anyMatch(c -> "Tricia Marie".equals(c.getFirstname())));
115+
}
116+
117+
@Test
118+
@WithMockUser(username = "userC", roles = "USER")
119+
void getContacts_asUserWithNoContacts_returnsEmptyList() {
120+
List<Contact> contacts = contactService.getContacts();
121+
122+
assertNotNull(contacts);
123+
assertTrue(contacts.isEmpty());
124+
}
125+
126+
@Test
127+
@WithMockUser(username = "userA", roles = "ADMIN")
128+
void getContacts_withWrongRole_throwsAccessDenied() {
129+
assertThrows(AccessDeniedException.class, () -> contactService.getContacts());
130+
}
131+
}

0 commit comments

Comments
 (0)