package io.conducive.server.db.impl;
import io.conducive.server.db.UserDAO;
import io.conducive.shared.model.User;
import io.conducive.server.resources.UserResource;
import io.conducive.server.wiring.SimpleAuthenticator;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.yammer.dropwizard.auth.basic.BasicAuthProvider;
import com.yammer.dropwizard.testing.ResourceTest;
import org.junit.Test;
import javax.ws.rs.core.MediaType;
import static com.google.common.hash.Hashing.sha256;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/**
*
*/
public class UserResourceTest extends ResourceTest {
UserDAO dao = mock(UserDAO.class);
@Override
protected void setUpResources() throws Exception {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
binder().bind(UserDAO.class).toInstance(dao);
}
});
addResource(injector.getInstance(UserResource.class));
addProvider(new BasicAuthProvider(injector.getInstance(SimpleAuthenticator.class), "authrealm"));
}
@Test
public void testCreate() throws Exception {
final User user = new User();
user.setUsername("reuben");
String hash = sha256().hashBytes("foo".getBytes("UTF-8")).toString().substring(0, 64);
String salt = "banana";
user.setHash(hash);
user.setSalt(salt);
client().resource("/user").type(MediaType.APPLICATION_JSON_TYPE).put(user);
verify(dao).createUser(user);
// test get
when(dao.getUser("reuben", hash, salt)).thenReturn(user);
client().addFilter(new HTTPBasicAuthFilter("reuben", hash + salt));
assertTrue(client().resource("/user").get(Boolean.class));
}
}