Skip to content

Commit

Permalink
yegor256#42 created unit test for PsGitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
dmzaytsev committed Apr 30, 2015
1 parent ffba83c commit 658d856
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 8 deletions.
54 changes: 46 additions & 8 deletions src/main/java/org/takes/facets/auth/social/PsGithub.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.HttpURLConnection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.json.JsonObject;
Expand Down Expand Up @@ -65,14 +66,31 @@ public final class PsGithub implements Pass {
*/
private final transient String key;

/**
* Urls replacements.
*/
private final transient Map<String, String> substs;

/**
* Ctor.
* @param gapp Github app
* @param gkey Github key
*/
public PsGithub(final String gapp, final String gkey) {
this(gapp, gkey, Collections.<String, String>emptyMap());
}

/**
* Ctor.
* @param gapp Github app
* @param gkey Github key
* @param urls Urls replacements
*/
PsGithub(final String gapp, final String gkey,
final Map<String, String> urls) {
this.app = gapp;
this.key = gkey;
this.substs = new ConcurrentHashMap<String, String>(urls);
}

@Override
Expand All @@ -84,7 +102,7 @@ public Iterator<Identity> enter(final Request request)
throw new IllegalArgumentException("code is not provided");
}
return Collections.singleton(
PsGithub.fetch(this.token(href.toString(), code.next()))
this.fetch(this.token(href.toString(), code.next()))
).iterator();
}

Expand All @@ -100,10 +118,13 @@ public Response exit(final Response response,
* @return The user found in Github
* @throws IOException If fails
*/
private static Identity fetch(final String token) throws IOException {
final String uri = new Href("https://api.github.com/user")
.with("access_token", token)
.toString();
private Identity fetch(final String token) throws IOException {
final String uri = new Href(
String.format(
"%s/user",
this.getUrl("https://api.github.com")
)
).with("access_token", token).toString();
return PsGithub.parse(
new JdkRequest(uri)
.header("accept", "application/json")
Expand All @@ -122,9 +143,12 @@ private static Identity fetch(final String token) throws IOException {
*/
private String token(final String home, final String code)
throws IOException {
// @checkstyle LineLength (1 line)
final String uri = new Href("https://github.com/login/oauth/access_token")
.with("client_id", this.app)
final String uri = new Href(
String.format(
"%s/login/oauth/access_token",
this.getUrl("https://github.com")
)
).with("client_id", this.app)
.with("redirect_uri", home)
.with("client_secret", this.key)
.with("code", code)
Expand Down Expand Up @@ -154,4 +178,18 @@ private static Identity parse(final JsonObject json) {
);
}

/**
* Take url from substitution map if exists.
* @param url Url
* @return Overridden url
*/
private String getUrl(final String url) {
final String ret;
if (this.substs.containsKey(url)) {
ret = this.substs.get(url);
} else {
ret = url;
}
return ret;
}
}
140 changes: 140 additions & 0 deletions src/test/java/org/takes/facets/auth/social/PsGithubTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.facets.auth.social;

import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import javax.json.Json;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.facets.auth.Identity;
import org.takes.facets.fork.FkRegex;
import org.takes.facets.fork.TkFork;
import org.takes.http.FtRemote;
import org.takes.rq.RqFake;
import org.takes.rq.RqHref;
import org.takes.rs.RsJSON;
import org.takes.rs.xe.RsXembly;
import org.takes.rs.xe.XeDirectives;
import org.xembly.Directives;

/**
* Test case for {@link org.takes.rq.RqMethod}.
* @author Dmitry Zaytsev ([email protected])
* @version $Id$
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
* @since 0.14.3
*/
public final class PsGithubTest {
/**
* PsGithub can login.
* @throws IOException If some problem inside
* @checkstyle MultipleStringLiteralsCheck (500 lines)
*/
@Test
public void canLogin() throws IOException {
final String token = "GitHubToken";
final String code = "GitHubCode";
final int userid = 1;
final String login = "octocat";
final String avatar = "https://github.com/images/error/octocat.gif";
final String lgattr = "login";
final String acattr = "access_token";
final Take take = new TkFork(
new FkRegex(
"/login/oauth/access_token",
new Take() {
@Override
public Response act(final Request req) throws IOException {
return new RsXembly(
new XeDirectives(
new Directives().add("OAuth")
.add("token_type").set("bearer").up()
.add("scope").set("repo,gist").up()
.add(acattr).set(token).toString()
)
);
}
}
),
new FkRegex(
"/user",
new Take() {
@Override
public Response act(final Request req) throws IOException {
MatcherAssert.assertThat(
new RqHref.Base(req).href().param(
acattr
).iterator().next(),
Matchers.containsString(token)
);
return new RsJSON(
Json.createObjectBuilder()
.add(lgattr, login)
.add("id", userid)
.add("avatar_url", avatar)
.build()
);
}
}
)
);
new FtRemote(take).exec(
// @checkstyle AnonInnerLengthCheck (100 lines)
new FtRemote.Script() {
@Override
public void exec(final URI home) throws IOException {
final ConcurrentHashMap<String, String> map =
new ConcurrentHashMap<String, String>(2);
map.put("https://github.com", home.toString());
map.put("https://api.github.com", home.toString());
final Iterator<Identity> itr = new PsGithub(
"app",
"key",
map
).enter(new RqFake("GET", String.format("?code=%s", code)));
final Identity identity = itr.next();
MatcherAssert.assertThat(
identity.urn(),
Matchers.equalTo(String.format("urn:github:%d", userid))
);
MatcherAssert.assertThat(
identity.properties().get(lgattr),
Matchers.equalTo(login)
);
MatcherAssert.assertThat(
identity.properties().get("avatar"),
Matchers.equalTo(avatar)
);
}
}
);
}
}

0 comments on commit 658d856

Please sign in to comment.