Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Mar 9, 2016
2 parents 2708700 + 92faa70 commit 4cbd7fe
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 26 deletions.
43 changes: 24 additions & 19 deletions src/main/java/org/takes/facets/auth/PsBasic.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
* @since 0.20
*/
@EqualsAndHashCode(of = { "entry", "realm" })
@SuppressWarnings("PMD.TooManyMethods")
public final class PsBasic implements Pass {

/**
Expand Down Expand Up @@ -156,7 +157,6 @@ public interface Entry {
* @author Endrigo Antonini (teamed@endrigo.com.br)
* @version $Id$
* @since 0.20
*
*/
public static final class Fake implements PsBasic.Entry {
/**
Expand Down Expand Up @@ -228,27 +228,11 @@ public static final class Default implements PsBasic.Entry {
* space characters as separators. Each of login, password and urn
* are URL-encoded substrings. For example,
* {@code "mike my%20password urn:jcabi-users:michael"}.
* @todo #558:30min Default ctor. According to new qulice version,
* constructor must contain only variables initialization and
* other constructor calls. Refactor code according to that rule
* and remove `ConstructorOnlyInitializesOrCallOtherConstructors`
* warning suppression.
*/
@SuppressWarnings
(
"PMD.ConstructorOnlyInitializesOrCallOtherConstructors"
)
public Default(final String... users) {
this.usernames = new HashMap<>(users.length);
for (final String user : users) {
final String unified = user.replace("%20", "+");
PsBasic.Default.validateUser(unified);
this.usernames.put(
PsBasic.Default.key(unified),
unified.substring(unified.lastIndexOf(' ') + 1)
);
}
this.usernames = Default.converted(users);
}

@Override
public Opt<Identity> enter(final String user, final String pwd) {
final Opt<String> urn = this.urn(user, pwd);
Expand All @@ -270,6 +254,27 @@ public Opt<Identity> enter(final String user, final String pwd) {
}
return identity;
}
/**
* Converts Strings with user's login, password and URN to Map.
* @param users Strings with user's login, password and URN with
* space characters as separators. Each of login, password and urn
* are URL-encoded substrings. For example,
* {@code "mike my%20password urn:jcabi-users:michael"}.
* @return Map from login/password pairs to URNs.
*/
private static Map<String, String> converted(final String... users) {
final Map<String, String> result =
new HashMap<String, String>(users.length);
for (final String user : users) {
final String unified = user.replace("%20", "+");
PsBasic.Default.validateUser(unified);
result.put(
PsBasic.Default.key(unified),
unified.substring(unified.lastIndexOf(' ') + 1)
);
}
return result;
}
/**
* Returns an URN corresponding to a login-password pair.
* @param user Login.
Expand Down
52 changes: 45 additions & 7 deletions src/test/java/org/takes/facets/auth/PsBasicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.takes.facets.auth;

import com.jcabi.aspects.Tv;
import javax.xml.bind.DatatypeConverter;
import org.apache.commons.lang.RandomStringUtils;
import org.hamcrest.CoreMatchers;
Expand Down Expand Up @@ -55,6 +56,11 @@ public final class PsBasicTest {
*/
private static final String AUTH_BASIC = "Authorization: Basic %s";

/**
* Valid code parameter.
*/
private static final String VALID_CODE = "?valid_code=%s";

/**
* PsBasic can handle connection with valid credential.
* @throws Exception if any error occurs
Expand All @@ -70,9 +76,8 @@ public void handleConnectionWithValidCredential() throws Exception {
new RqFake(
RqMethod.GET,
String.format(
"?valid_code=%s",
// @checkstyle MagicNumberCheck (1 line)
RandomStringUtils.randomAlphanumeric(10)
PsBasicTest.VALID_CODE,
RandomStringUtils.randomAlphanumeric(Tv.TEN)
)
),
PsBasicTest.header(user, "pass")
Expand All @@ -85,6 +90,41 @@ public void handleConnectionWithValidCredential() throws Exception {
);
}

/**
* PsBasic can handle connection with valid credential when Entry is
* a instance of Default.
* @throws Exception if any error occurs
*/
@Test
public void handleConnectionWithValidCredentialDefaultEntry()
throws Exception {
final String user = "johny";
final String password = "password2";
final Opt<Identity> identity = new PsBasic(
"RealmAA",
new PsBasic.Default(
"mike my%20password1 urn:basic:michael",
String.format("%s %s urn:basic:%s", user, password, user)
)
).enter(
new RqWithHeaders(
new RqFake(
RqMethod.GET,
String.format(
PsBasicTest.VALID_CODE,
RandomStringUtils.randomAlphanumeric(Tv.TEN)
)
),
PsBasicTest.header(user, password)
)
);
MatcherAssert.assertThat(identity.has(), Matchers.is(true));
MatcherAssert.assertThat(
identity.get().urn(),
CoreMatchers.equalTo(PsBasicTest.urn(user))
);
}

/**
* PsBasic can handle connection with invalid credential.
* @throws Exception If some problem inside
Expand All @@ -102,8 +142,7 @@ public void handleConnectionWithInvalidCredential() throws Exception {
RqMethod.GET,
String.format(
"?invalid_code=%s",
// @checkstyle MagicNumberCheck (1 line)
RandomStringUtils.randomAlphanumeric(10)
RandomStringUtils.randomAlphanumeric(Tv.TEN)
)
),
PsBasicTest.header("username", "wrong")
Expand Down Expand Up @@ -139,8 +178,7 @@ public void handleMultipleHeadersWithValidCredential() throws Exception {
RqMethod.GET,
String.format(
"?multiple_code=%s",
// @checkstyle MagicNumberCheck (1 line)
RandomStringUtils.randomAlphanumeric(10)
RandomStringUtils.randomAlphanumeric(Tv.TEN)
)
),
PsBasicTest.header(user, "changeit"),
Expand Down

0 comments on commit 4cbd7fe

Please sign in to comment.