-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
336 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
niffler-e-2-e-tests/src/test/java/guru/qa/niffler/api/core/CodeInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package guru.qa.niffler.api.core; | ||
|
||
import guru.qa.niffler.jupiter.extension.ApiLoginExtension; | ||
import okhttp3.Interceptor; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class CodeInterceptor implements Interceptor { | ||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
final Response response = chain.proceed(chain.request()); | ||
if (response.isRedirect()) { | ||
String location = Objects.requireNonNull( | ||
response.header("Location") | ||
); | ||
if (location.contains("code=")) { | ||
ApiLoginExtension.setCode( | ||
StringUtils.substringAfter( | ||
location, "code=" | ||
) | ||
); | ||
} | ||
} | ||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
niffler-e-2-e-tests/src/test/java/guru/qa/niffler/jupiter/annotation/ApiLogin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package guru.qa.niffler.jupiter.annotation; | ||
|
||
import guru.qa.niffler.jupiter.extension.ApiLoginExtension; | ||
import guru.qa.niffler.jupiter.extension.UserExtension; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
@ExtendWith({ | ||
UserExtension.class, | ||
ApiLoginExtension.class | ||
}) | ||
public @interface ApiLogin { | ||
String username() default ""; | ||
String password() default ""; | ||
} |
11 changes: 11 additions & 0 deletions
11
niffler-e-2-e-tests/src/test/java/guru/qa/niffler/jupiter/annotation/Token.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package guru.qa.niffler.jupiter.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.PARAMETER) | ||
public @interface Token { | ||
} |
120 changes: 120 additions & 0 deletions
120
niffler-e-2-e-tests/src/test/java/guru/qa/niffler/jupiter/extension/ApiLoginExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package guru.qa.niffler.jupiter.extension; | ||
|
||
import com.codeborne.selenide.Selenide; | ||
import com.codeborne.selenide.WebDriverRunner; | ||
import guru.qa.niffler.api.core.ThreadSafeCookieStore; | ||
import guru.qa.niffler.config.Config; | ||
import guru.qa.niffler.jupiter.annotation.ApiLogin; | ||
import guru.qa.niffler.jupiter.annotation.Token; | ||
import guru.qa.niffler.model.rest.TestData; | ||
import guru.qa.niffler.model.rest.UserJson; | ||
import guru.qa.niffler.page.MainPage; | ||
import guru.qa.niffler.service.impl.AuthApiClient; | ||
import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.jupiter.api.extension.ParameterResolutionException; | ||
import org.junit.jupiter.api.extension.ParameterResolver; | ||
import org.junit.platform.commons.support.AnnotationSupport; | ||
import org.openqa.selenium.Cookie; | ||
|
||
|
||
public class ApiLoginExtension implements BeforeEachCallback, ParameterResolver { | ||
|
||
private static final Config CFG = Config.getInstance(); | ||
public static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(ApiLoginExtension.class); | ||
|
||
private final AuthApiClient authApiClient = new AuthApiClient(); | ||
private final boolean setupBrowser; | ||
|
||
private ApiLoginExtension(boolean setupBrowser) { | ||
this.setupBrowser = setupBrowser; | ||
} | ||
|
||
public ApiLoginExtension() { | ||
this.setupBrowser = true; | ||
} | ||
|
||
public static ApiLoginExtension restApiLoginExtension() { | ||
return new ApiLoginExtension(false); | ||
} | ||
|
||
@Override | ||
public void beforeEach(ExtensionContext context) throws Exception { | ||
AnnotationSupport.findAnnotation(context.getRequiredTestMethod(), ApiLogin.class) | ||
.ifPresent(apiLogin -> { | ||
|
||
final UserJson userToLogin; | ||
final UserJson userFromUserExtension = UserExtension.getUserJson(); | ||
if ("".equals(apiLogin.username()) || "".equals(apiLogin.password())) { | ||
if (userFromUserExtension == null) { | ||
throw new IllegalStateException("@User must be present in case that @ApiLogin is empty!"); | ||
} | ||
userToLogin = userFromUserExtension; | ||
} else { | ||
UserJson fakeUser = new UserJson( | ||
apiLogin.username(), | ||
new TestData( | ||
apiLogin.password() | ||
) | ||
); | ||
if (userFromUserExtension != null) { | ||
throw new IllegalStateException("@User must not be present in case that @ApiLogin contains username or password!"); | ||
} | ||
UserExtension.setUser(fakeUser); | ||
userToLogin = fakeUser; | ||
} | ||
|
||
final String token = authApiClient.login( | ||
userToLogin.username(), | ||
userToLogin.testData().password() | ||
); | ||
setToken(token); | ||
if (setupBrowser) { | ||
Selenide.open(CFG.frontUrl()); | ||
Selenide.localStorage().setItem("id_token", getToken()); | ||
WebDriverRunner.getWebDriver().manage().addCookie( | ||
new Cookie( | ||
"JSESSIONID", | ||
ThreadSafeCookieStore.INSTANCE.cookieValue("JSESSIONID") | ||
) | ||
); | ||
Selenide.open(MainPage.URL, MainPage.class).checkThatPageLoaded(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { | ||
return parameterContext.getParameter().getType().isAssignableFrom(String.class) | ||
&& AnnotationSupport.isAnnotated(parameterContext.getParameter(), Token.class); | ||
} | ||
|
||
@Override | ||
public String resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { | ||
return getToken(); | ||
} | ||
|
||
public static void setToken(String token) { | ||
TestMethodContextExtension.context().getStore(NAMESPACE).put("token", token); | ||
} | ||
|
||
public static String getToken() { | ||
return TestMethodContextExtension.context().getStore(NAMESPACE).get("token", String.class); | ||
} | ||
|
||
public static void setCode(String code) { | ||
TestMethodContextExtension.context().getStore(NAMESPACE).put("code", code); | ||
} | ||
|
||
public static String getCode() { | ||
return TestMethodContextExtension.context().getStore(NAMESPACE).get("code", String.class); | ||
} | ||
|
||
public static Cookie getJsessionIdCookie() { | ||
return new Cookie( | ||
"JSESSIONID", | ||
ThreadSafeCookieStore.INSTANCE.cookieValue("JSESSIONID") | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
niffler-e-2-e-tests/src/test/java/guru/qa/niffler/service/impl/AuthApiClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package guru.qa.niffler.service.impl; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import guru.qa.niffler.api.AuthApi; | ||
import guru.qa.niffler.api.core.CodeInterceptor; | ||
import guru.qa.niffler.api.core.RestClient; | ||
import guru.qa.niffler.api.core.ThreadSafeCookieStore; | ||
import guru.qa.niffler.config.Config; | ||
import guru.qa.niffler.jupiter.extension.ApiLoginExtension; | ||
import guru.qa.niffler.utils.OAuthUtils; | ||
import lombok.SneakyThrows; | ||
import retrofit2.Response; | ||
|
||
|
||
public class AuthApiClient extends RestClient { | ||
|
||
private static final Config CFG = Config.getInstance(); | ||
private final AuthApi authApi; | ||
|
||
public AuthApiClient() { | ||
super(CFG.authUrl(), true, new CodeInterceptor()); | ||
this.authApi = create(AuthApi.class); | ||
} | ||
|
||
@SneakyThrows | ||
public String login(String username, String password) { | ||
final String codeVerifier = OAuthUtils.generateCodeVerifier(); | ||
final String codeChallenge = OAuthUtils.generateCodeChallange(codeVerifier); | ||
final String redirectUri = CFG.frontUrl() + "authorized"; | ||
final String clientId = "client"; | ||
|
||
authApi.authorize( | ||
"code", | ||
clientId, | ||
"openid", | ||
redirectUri, | ||
codeChallenge, | ||
"S256" | ||
).execute(); | ||
|
||
authApi.login( | ||
username, | ||
password, | ||
ThreadSafeCookieStore.INSTANCE.cookieValue("XSRF-TOKEN") | ||
).execute(); | ||
|
||
Response<JsonNode> tokenResponse = authApi.token( | ||
ApiLoginExtension.getCode(), | ||
redirectUri, | ||
clientId, | ||
codeVerifier, | ||
"authorization_code" | ||
).execute(); | ||
|
||
return tokenResponse.body().get("id_token").asText(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...va/guru/qa/niffler/test/web/JdbcTest.java → ...a/guru/qa/niffler/test/fake/JdbcTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
niffler-e-2-e-tests/src/test/java/guru/qa/niffler/test/fake/OAuthTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package guru.qa.niffler.test.fake; | ||
|
||
import guru.qa.niffler.config.Config; | ||
import guru.qa.niffler.jupiter.annotation.ApiLogin; | ||
import guru.qa.niffler.jupiter.annotation.Token; | ||
import guru.qa.niffler.model.rest.UserJson; | ||
import guru.qa.niffler.service.impl.AuthApiClient; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class OAuthTest { | ||
|
||
private static final Config CFG = Config.getInstance(); | ||
private final AuthApiClient authApiClient = new AuthApiClient(); | ||
|
||
@Test | ||
@ApiLogin(username = "duck", password = "12345") | ||
void oauthTest(@Token String token, UserJson user) { | ||
System.out.println(user); | ||
Assertions.assertNotNull(token); | ||
} | ||
} |
Oops, something went wrong.