001: /*
002: * Created on 09/09/2006 17:00:27
003: */
004: package net.jforum.api.integration.rest;
005:
006: import java.sql.PreparedStatement;
007: import java.sql.SQLException;
008: import java.sql.Timestamp;
009: import java.util.Calendar;
010: import java.util.Date;
011: import java.util.GregorianCalendar;
012:
013: import junit.framework.TestCase;
014: import net.jforum.JForumExecutionContext;
015: import net.jforum.TestCaseUtils;
016: import net.jforum.api.rest.RESTAuthentication;
017:
018: /**
019: * @author Rafael Steil
020: * @version $Id: RESTAuthenticationTestCase.java,v 1.4 2007/07/28 14:17:11 rafaelsteil Exp $
021: */
022: public class RESTAuthenticationTestCase extends TestCase {
023: public static final String API_KEY = "api.key.test";
024:
025: public void testInvalid() throws Exception {
026: RESTAuthentication auth = new RESTAuthentication();
027: boolean isValid = auth.validateApiKey("1");
028:
029: assertFalse("The api key should not be valid", isValid);
030: }
031:
032: public void testValid() throws Exception {
033: RESTAuthentication auth = new RESTAuthentication();
034: boolean isValid = auth.validateApiKey(API_KEY);
035:
036: assertTrue("The api key should be valid", isValid);
037: }
038:
039: Date tomorrow() {
040: Calendar c = Calendar.getInstance();
041: return new GregorianCalendar(c.get(Calendar.YEAR), c
042: .get(Calendar.MONTH), c.get(Calendar.DATE) + 1)
043: .getTime();
044: }
045:
046: /**
047: * @throws SQLException
048: */
049: void createApiKey(Date validity) throws SQLException {
050: PreparedStatement p = null;
051:
052: try {
053: p = JForumExecutionContext.getConnection()
054: .prepareStatement(
055: "INSERT INTO jforum_api (api_key, api_validity) "
056: + " VALUES (?, ?)");
057: p.setString(1, API_KEY);
058: p.setTimestamp(2, new Timestamp(validity.getTime()));
059: p.executeUpdate();
060: } finally {
061: if (p != null)
062: p.close();
063: }
064: }
065:
066: /**
067: * @throws SQLException
068: */
069: void deleteApiKey() throws SQLException {
070: PreparedStatement p = null;
071:
072: try {
073: p = JForumExecutionContext.getConnection()
074: .prepareStatement(
075: "DELETE FROM jforum_api WHERE api_key = ?");
076: p.setString(1, API_KEY);
077: p.executeUpdate();
078: } finally {
079: if (p != null)
080: p.close();
081: }
082: }
083:
084: /**
085: * @see junit.framework.TestCase#setUp()
086: */
087: protected void setUp() throws Exception {
088: TestCaseUtils.loadEnvironment();
089: TestCaseUtils.initDatabaseImplementation();
090: this .createApiKey(this .tomorrow());
091: }
092:
093: /**
094: * @see junit.framework.TestCase#tearDown()
095: */
096: protected void tearDown() throws Exception {
097: this.deleteApiKey();
098: JForumExecutionContext.finish();
099: }
100: }
|