14 changed files with 6 additions and 382 deletions
@ -1,37 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-2021 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed 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 |
|
||||||
* |
|
||||||
* https://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 smoketest.graphql; |
|
||||||
|
|
||||||
import org.springframework.graphql.data.method.annotation.Argument; |
|
||||||
import org.springframework.graphql.data.method.annotation.QueryMapping; |
|
||||||
import org.springframework.stereotype.Controller; |
|
||||||
|
|
||||||
@Controller |
|
||||||
public class GreetingController { |
|
||||||
|
|
||||||
private final GreetingService greetingService; |
|
||||||
|
|
||||||
public GreetingController(GreetingService greetingService) { |
|
||||||
this.greetingService = greetingService; |
|
||||||
} |
|
||||||
|
|
||||||
@QueryMapping |
|
||||||
public String greeting(@Argument String name) { |
|
||||||
return this.greetingService.greet(name); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,30 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-2021 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed 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 |
|
||||||
* |
|
||||||
* https://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 smoketest.graphql; |
|
||||||
|
|
||||||
import org.springframework.security.access.prepost.PreAuthorize; |
|
||||||
import org.springframework.stereotype.Component; |
|
||||||
|
|
||||||
@Component |
|
||||||
public class GreetingService { |
|
||||||
|
|
||||||
@PreAuthorize("hasRole('ADMIN')") |
|
||||||
public String greet(String name) { |
|
||||||
return "Hello, " + name + "!"; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,65 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-2021 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed 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 |
|
||||||
* |
|
||||||
* https://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 smoketest.graphql; |
|
||||||
|
|
||||||
import java.util.Objects; |
|
||||||
|
|
||||||
public class Project { |
|
||||||
|
|
||||||
private String slug; |
|
||||||
|
|
||||||
private String name; |
|
||||||
|
|
||||||
public Project(String slug, String name) { |
|
||||||
this.slug = slug; |
|
||||||
this.name = name; |
|
||||||
} |
|
||||||
|
|
||||||
public String getSlug() { |
|
||||||
return this.slug; |
|
||||||
} |
|
||||||
|
|
||||||
public void setSlug(String slug) { |
|
||||||
this.slug = slug; |
|
||||||
} |
|
||||||
|
|
||||||
public String getName() { |
|
||||||
return this.name; |
|
||||||
} |
|
||||||
|
|
||||||
public void setName(String name) { |
|
||||||
this.name = name; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean equals(Object o) { |
|
||||||
if (this == o) { |
|
||||||
return true; |
|
||||||
} |
|
||||||
if (o == null || getClass() != o.getClass()) { |
|
||||||
return false; |
|
||||||
} |
|
||||||
Project project = (Project) o; |
|
||||||
return this.slug.equals(project.slug); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public int hashCode() { |
|
||||||
return Objects.hash(this.slug); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,42 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-2022 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed 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 |
|
||||||
* |
|
||||||
* https://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 smoketest.graphql; |
|
||||||
|
|
||||||
import java.util.Arrays; |
|
||||||
import java.util.List; |
|
||||||
import java.util.Optional; |
|
||||||
|
|
||||||
import org.springframework.graphql.data.method.annotation.Argument; |
|
||||||
import org.springframework.graphql.data.method.annotation.QueryMapping; |
|
||||||
import org.springframework.stereotype.Controller; |
|
||||||
|
|
||||||
@Controller |
|
||||||
public class ProjectController { |
|
||||||
|
|
||||||
private final List<Project> projects; |
|
||||||
|
|
||||||
public ProjectController() { |
|
||||||
this.projects = Arrays.asList(new Project("spring-boot", "Spring Boot"), |
|
||||||
new Project("spring-graphql", "Spring GraphQL"), new Project("spring-framework", "Spring Framework")); |
|
||||||
} |
|
||||||
|
|
||||||
@QueryMapping |
|
||||||
public Optional<Project> project(@Argument String slug) { |
|
||||||
return this.projects.stream().filter((project) -> project.getSlug().equals(slug)).findFirst(); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,29 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-2021 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed 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 |
|
||||||
* |
|
||||||
* https://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 smoketest.graphql; |
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication; |
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication; |
|
||||||
|
|
||||||
@SpringBootApplication |
|
||||||
public class SampleGraphQlApplication { |
|
||||||
|
|
||||||
public static void main(String[] args) { |
|
||||||
SpringApplication.run(SampleGraphQlApplication.class, args); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,53 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-2022 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed 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 |
|
||||||
* |
|
||||||
* https://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 smoketest.graphql; |
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean; |
|
||||||
import org.springframework.context.annotation.Configuration; |
|
||||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; |
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
|
||||||
import org.springframework.security.core.userdetails.User; |
|
||||||
import org.springframework.security.core.userdetails.UserDetails; |
|
||||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
|
||||||
import org.springframework.security.web.DefaultSecurityFilterChain; |
|
||||||
|
|
||||||
import static org.springframework.security.config.Customizer.withDefaults; |
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false) |
|
||||||
@EnableWebSecurity |
|
||||||
@EnableGlobalMethodSecurity(prePostEnabled = true) |
|
||||||
public class SecurityConfig { |
|
||||||
|
|
||||||
@Bean |
|
||||||
public DefaultSecurityFilterChain springWebFilterChain(HttpSecurity http) throws Exception { |
|
||||||
return http.csrf((csrf) -> csrf.disable()) |
|
||||||
// Demonstrate that method security works
|
|
||||||
// Best practice to use both for defense in depth
|
|
||||||
.authorizeRequests((requests) -> requests.anyRequest().permitAll()).httpBasic(withDefaults()).build(); |
|
||||||
} |
|
||||||
|
|
||||||
@Bean |
|
||||||
@SuppressWarnings("deprecation") |
|
||||||
public InMemoryUserDetailsManager userDetailsService() { |
|
||||||
User.UserBuilder userBuilder = User.withDefaultPasswordEncoder(); |
|
||||||
UserDetails rob = userBuilder.username("rob").password("rob").roles("USER").build(); |
|
||||||
UserDetails admin = userBuilder.username("admin").password("admin").roles("USER", "ADMIN").build(); |
|
||||||
return new InMemoryUserDetailsManager(rob, admin); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,65 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-2022 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed 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 |
|
||||||
* |
|
||||||
* https://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 smoketest.graphql; |
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test; |
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||||
import org.springframework.boot.test.autoconfigure.graphql.tester.AutoConfigureHttpGraphQlTester; |
|
||||||
import org.springframework.boot.test.context.SpringBootTest; |
|
||||||
import org.springframework.graphql.execution.ErrorType; |
|
||||||
import org.springframework.graphql.test.tester.HttpGraphQlTester; |
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat; |
|
||||||
|
|
||||||
@SpringBootTest |
|
||||||
@AutoConfigureHttpGraphQlTester |
|
||||||
class GreetingControllerTests { |
|
||||||
|
|
||||||
@Autowired |
|
||||||
private HttpGraphQlTester graphQlTester; |
|
||||||
|
|
||||||
@Test |
|
||||||
void shouldUnauthorizeAnonymousUsers() { |
|
||||||
this.graphQlTester.documentName("greeting").variable("name", "Brian").execute().errors().satisfy((errors) -> { |
|
||||||
assertThat(errors).hasSize(1); |
|
||||||
assertThat(errors.get(0).getErrorType()).isEqualTo(ErrorType.UNAUTHORIZED); |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
void shouldGreetWithSpecificName() { |
|
||||||
HttpGraphQlTester authenticated = withAdminCredentials(this.graphQlTester); |
|
||||||
authenticated.documentName("greeting").variable("name", "Brian").execute().path("greeting").entity(String.class) |
|
||||||
.isEqualTo("Hello, Brian!"); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
void shouldGreetWithDefaultName() { |
|
||||||
HttpGraphQlTester authenticated = withAdminCredentials(this.graphQlTester); |
|
||||||
authenticated.document("{ greeting }").execute().path("greeting").entity(String.class) |
|
||||||
.isEqualTo("Hello, Spring!"); |
|
||||||
} |
|
||||||
|
|
||||||
private HttpGraphQlTester withAdminCredentials(HttpGraphQlTester graphQlTester) { |
|
||||||
return graphQlTester.mutate() |
|
||||||
.webTestClient( |
|
||||||
(httpClient) -> httpClient.defaultHeaders((headers) -> headers.setBasicAuth("admin", "admin"))) |
|
||||||
.build(); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,43 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2012-2022 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed 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 |
|
||||||
* |
|
||||||
* https://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 smoketest.graphql; |
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test; |
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||||
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest; |
|
||||||
import org.springframework.graphql.test.tester.GraphQlTester; |
|
||||||
|
|
||||||
@GraphQlTest(ProjectController.class) |
|
||||||
class ProjectControllerTests { |
|
||||||
|
|
||||||
@Autowired |
|
||||||
private GraphQlTester graphQlTester; |
|
||||||
|
|
||||||
@Test |
|
||||||
void shouldFindSpringGraphQl() { |
|
||||||
this.graphQlTester.document("{ project(slug: \"spring-graphql\") { name } }").execute().path("project.name") |
|
||||||
.entity(String.class).isEqualTo("Spring GraphQL"); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
void shouldNotFindUnknownProject() { |
|
||||||
this.graphQlTester.document("{ project(slug: \"spring-unknown\") { name } }").execute().path("project.name") |
|
||||||
.pathDoesNotExist(); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
Loading…
Reference in new issue