Skip to content

Commit c639d0a

Browse files
committed
Add AOP Integration Test
Closes gh-14637
1 parent 1615553 commit c639d0a

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.annotation.method.configuration.issue14637;
18+
19+
import javax.sql.DataSource;
20+
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
24+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
25+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
26+
import org.springframework.orm.jpa.JpaTransactionManager;
27+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
28+
import org.springframework.orm.jpa.vendor.Database;
29+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
30+
import org.springframework.security.config.annotation.method.configuration.issue14637.domain.Entry;
31+
import org.springframework.transaction.PlatformTransactionManager;
32+
import org.springframework.transaction.annotation.EnableTransactionManagement;
33+
34+
/**
35+
* @author Josh Cummings
36+
*/
37+
@Configuration
38+
@EnableJpaRepositories("org.springframework.security.config.annotation.method.configuration.issue14637.repo")
39+
@EnableTransactionManagement
40+
public class ApplicationConfig {
41+
42+
@Bean
43+
public DataSource dataSource() {
44+
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
45+
return builder.setType(EmbeddedDatabaseType.HSQL).build();
46+
}
47+
48+
@Bean
49+
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
50+
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
51+
vendorAdapter.setDatabase(Database.HSQL);
52+
vendorAdapter.setGenerateDdl(true);
53+
vendorAdapter.setShowSql(true);
54+
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
55+
factory.setJpaVendorAdapter(vendorAdapter);
56+
factory.setPackagesToScan(Entry.class.getPackage().getName());
57+
factory.setDataSource(dataSource());
58+
return factory;
59+
}
60+
61+
@Bean
62+
public PlatformTransactionManager transactionManager() {
63+
JpaTransactionManager txManager = new JpaTransactionManager();
64+
txManager.setEntityManagerFactory(entityManagerFactory().getObject());
65+
return txManager;
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.annotation.method.configuration.issue14637;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.security.access.AccessDeniedException;
24+
import org.springframework.security.config.annotation.method.configuration.issue14637.domain.Entry;
25+
import org.springframework.security.config.annotation.method.configuration.issue14637.repo.EntryRepository;
26+
import org.springframework.security.test.context.support.WithMockUser;
27+
import org.springframework.test.context.ContextConfiguration;
28+
import org.springframework.test.context.junit.jupiter.SpringExtension;
29+
30+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
31+
32+
/**
33+
* @author Josh Cummings
34+
*/
35+
@ExtendWith(SpringExtension.class)
36+
@ContextConfiguration(classes = { ApplicationConfig.class, SecurityConfig.class })
37+
public class Issue14637Tests {
38+
39+
@Autowired
40+
private EntryRepository entries;
41+
42+
@Test
43+
@WithMockUser
44+
public void authenticateWhenInvalidPasswordThenBadCredentialsException() {
45+
Entry entry = new Entry();
46+
entry.setId(123L);
47+
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(() -> this.entries.save(entry));
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.annotation.method.configuration.issue14637;
18+
19+
import org.springframework.context.annotation.Configuration;
20+
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
21+
22+
/**
23+
* @author Josh Cummings
24+
*/
25+
@Configuration
26+
@EnableMethodSecurity
27+
public class SecurityConfig {
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.annotation.method.configuration.issue14637.domain;
18+
19+
import jakarta.persistence.Entity;
20+
import jakarta.persistence.GeneratedValue;
21+
import jakarta.persistence.GenerationType;
22+
import jakarta.persistence.Id;
23+
24+
/**
25+
* @author Josh Cummings
26+
*/
27+
@Entity
28+
public class Entry {
29+
30+
@Id
31+
@GeneratedValue(strategy = GenerationType.AUTO)
32+
private Long id;
33+
34+
public Long getId() {
35+
return this.id;
36+
}
37+
38+
public void setId(Long id) {
39+
this.id = id;
40+
}
41+
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.annotation.method.configuration.issue14637.repo;
18+
19+
import org.springframework.data.repository.CrudRepository;
20+
import org.springframework.security.access.prepost.PreAuthorize;
21+
import org.springframework.security.config.annotation.method.configuration.issue14637.domain.Entry;
22+
23+
/**
24+
* @author Josh Cummings
25+
*/
26+
public interface EntryRepository extends CrudRepository<Entry, String> {
27+
28+
@PreAuthorize("#entry.id == null")
29+
Entry save(Entry entry);
30+
31+
}

0 commit comments

Comments
 (0)