Skip to content

Instantly share code, notes, and snippets.

@elnur
Created April 23, 2013 20:34
Show Gist options
  • Select an option

  • Save elnur/5447137 to your computer and use it in GitHub Desktop.

Select an option

Save elnur/5447137 to your computer and use it in GitHub Desktop.
package functional;
import com.googlecode.flyway.core.Flyway;
import net.experium.manager.TokenManager;
import net.experium.model.Token;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static com.jayway.restassured.RestAssured.expect;
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:context.xml")
public class AuthenticationIT {
@Autowired
private TokenManager tokenManager;
@Autowired
private Flyway flyway;
@Before
public void prepareDatabase() {
flyway.clean();
flyway.migrate();
}
@Test
public void asksForTokenAuthentication() throws Exception {
expect().header("WWW-Authenticate", is(equalTo("Token"))).when().get("/");
}
@Test
public void returns401WhenNoAuthenticationIsProvided() throws Exception {
expect().statusCode(is(equalTo(401))).when().get("/");
}
@Test
public void returns401WhenEmptyTokenIsProvided() throws Exception {
given().header("Authorization", "Token ").expect().statusCode(is(equalTo(401))).when().get("/");
}
@Test
public void returns401WhenIncorrectTokenProvided() throws Exception {
given().header("Authorization", "Token no-such-token").expect().statusCode(is(equalTo(401))).when().get("/");
}
@Test
public void returns200WhenACorrectTokenProvided() throws Exception {
final String key = "valid-key";
final Token token = new Token(key);
tokenManager.save(token);
given().header("Authorization", "Token " + key).expect().statusCode(is(equalTo(200))).when().get("/");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment