Example #1
0
func TestExpirationMax(t *testing.T) {
	now := time.Now().UTC()

	obj := fill(t, now)
	obj.RespDirectives.MaxAge = DeltaSeconds(60)

	rv := ObjectResults{}
	ExpirationObject(&obj, &rv)
	require.Len(t, rv.OutWarnings, 0)
	require.WithinDuration(t, now.Add(time.Second*60), rv.OutExpirationTime, time.Second*1)
}
Example #2
0
func TestExpirationExpires(t *testing.T) {
	now := time.Now().UTC()

	obj := fill(t, now)
	// cache should select the SMax age since this is a shared cache.
	obj.RespExpiresHeader = now.Add(time.Second * 1500)

	rv := ObjectResults{}
	ExpirationObject(&obj, &rv)
	require.Len(t, rv.OutWarnings, 0)
	require.WithinDuration(t, now.Add(time.Second*1500), rv.OutExpirationTime, time.Second*1)
}
Example #3
0
func TestExpirationMaxAndSMax(t *testing.T) {
	now := time.Now().UTC()

	obj := fill(t, now)
	// cache should select the SMax age since this is a shared cache.
	obj.RespDirectives.MaxAge = DeltaSeconds(60)
	obj.RespDirectives.SMaxAge = DeltaSeconds(900)

	rv := ObjectResults{}
	ExpirationObject(&obj, &rv)
	require.Len(t, rv.OutWarnings, 0)
	require.WithinDuration(t, now.Add(time.Second*900), rv.OutExpirationTime, time.Second*1)
}
Example #4
0
func TestPostgresTokens(t *testing.T) {
	// Connect
	repo, err := getPostgresDB()
	require.NoError(t, err)

	expiry := time.Now().UTC()

	token := Token{UserID: 1, Value: "Myawesometoken", Expires: expiry}

	err = repo.SetToken(&token)
	require.NoError(t, err)

	t.Logf("Set token: %v", token)

	repoToken, err := repo.GetToken(token.Value)
	require.NoError(t, err)

	require.WithinDuration(t, expiry, repoToken.Expires, time.Duration(5*time.Second))
	require.Equal(t, "Myawesometoken", repoToken.Value)
	require.Equal(t, 1, repoToken.UserID)

	t.Logf("Got Token: %v", repoToken)

	err = repo.DelToken(repoToken.Value)
	require.NoError(t, err)

	deletedToken, err := repo.GetToken(token.Value)
	// It should have been deleted
	require.Nil(t, deletedToken)

	t.Log("Test Token deleted")

	// Expired already
	expiry = time.Now().UTC().Add(-time.Duration(2 * time.Hour))
	token = Token{UserID: 1, Value: "Myawesometoken", Expires: expiry}

	err = repo.SetToken(&token)
	require.NoError(t, err)

	// TODO : Implement delete all expired tokens
	/*err = repo.DelExpiredTokens()
	require.NoError(t, err)

	repoToken, err = repo.GetToken(token.Value)
	// It should have been deleted as it is an expired token
	require.Nil(t, repoToken)*/
}
Example #5
0
func TestCachableResponsePublic(t *testing.T) {
	req, res := roundTrip(t, func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		w.Header().Set("Cache-Control", "public")
		w.Header().Set("Last-Modified",
			time.Now().UTC().Add(time.Duration(time.Hour*-5)).Format(http.TimeFormat))
		fmt.Fprintln(w, `{}`)
	})

	reasons, expires, err := UsingRequestResponse(req, res.StatusCode, res.Header, false)

	require.NoError(t, err)
	require.Len(t, reasons, 0)
	require.WithinDuration(t,
		time.Now().UTC().Add(time.Duration(float64(time.Hour)*0.5)),
		expires,
		10*time.Second)
}
Example #6
0
func TestHEADLongLastModified(t *testing.T) {
	now := time.Now().UTC()

	obj := fill(t, now)
	obj.ReqMethod = "HEAD"
	obj.RespLastModifiedHeader = now.Add(time.Hour * -70000)

	rv := ObjectResults{}
	CachableObject(&obj, &rv)
	require.NoError(t, rv.OutErr)
	require.Len(t, rv.OutReasons, 0)

	ExpirationObject(&obj, &rv)
	require.NoError(t, rv.OutErr)
	require.Len(t, rv.OutReasons, 0)
	require.False(t, rv.OutExpirationTime.IsZero())
	require.WithinDuration(t, now.Add(twentyFourHours), rv.OutExpirationTime, time.Second*60)
}
Example #7
0
func TestPostgresApplications(t *testing.T) {

	// Connect
	repo, err := getPostgresDB()
	require.NoError(t, err)

	t.Logf("Creating test applicant user")
	emailAddress := fmt.Sprintf("test_admin_%s@%s.com", GetRandomString(5, ""), GetRandomString(5, ""))
	firstName := "Iwannabe"
	lastName := "Astudent"
	created := time.Now().UTC()
	password := "******"
	t.Logf("Password: '******'", password)
	bcryptPassword, err := createHashedPassword(password)
	t.Logf("hashPassword: %s", bcryptPassword)
	require.NoError(t, err)
	user := User{EmailAddress: emailAddress, FirstName: firstName, LastName: lastName, Password: bcryptPassword, Created: created, Role: RoleAdmin}

	t.Logf("Adding user: %v", user)

	err = repo.SetUser(&user)
	require.NoError(t, err)

	repoUser, err := repo.GetUserByEmail(emailAddress)
	require.NoError(t, err)

	appl := Application{
		Birthday:              created,
		PhoneNumber:           "555",
		Nationality:           "marsian",
		Country:               "for old men",
		City:                  "atlantis",
		Zip:                   "666",
		Address:               "none",
		AddressExtra:          "of yo business",
		FirstPageOfSurveyData: "I use a GameBoy",
		Gender:                "female",
		UserID:                repoUser.ID,
		EducationLevel:        2,
		Status:                "rejected",
		BlockExpires:          created,
		Created:               created,
		Edited:                created}

	err = repo.SetApplication(&appl)
	require.NoError(t, err)

	repoAppl, err := repo.GetApplicationOf(repoUser.ID)
	require.NoError(t, err)

	t.Logf("Set application: %v", repoAppl)

	// Birthday is stored as date not timestamp
	require.WithinDuration(t, created, repoAppl.Birthday, time.Duration(24*time.Hour))
	require.Equal(t, "555", repoAppl.PhoneNumber)
	require.Equal(t, "for old men", repoAppl.Country)
	require.Equal(t, "marsian", repoAppl.Nationality)
	require.Equal(t, "none", repoAppl.Address)
	require.Equal(t, "of yo business", repoAppl.AddressExtra)
	require.Equal(t, "atlantis", repoAppl.City)
	require.Equal(t, "female", repoAppl.Gender)
	require.Equal(t, "I use a GameBoy", repoAppl.FirstPageOfSurveyData)
	require.Equal(t, "rejected", repoAppl.Status)
	require.Equal(t, "female", repoAppl.Gender)
	require.WithinDuration(t, created, repoAppl.Created, time.Duration(5*time.Second))
	require.WithinDuration(t, created, repoAppl.Edited, time.Duration(5*time.Second))
	require.WithinDuration(t, created, repoAppl.BlockExpires, time.Duration(5*time.Second))
	require.True(t, repoAppl.ID > 0)

	t.Logf("Got Application: %v", repoAppl)

	repoAppl.Nationality = "venusian"
	err = repo.UpdateApplication(repoAppl)
	require.NoError(t, err)

	t.Logf("Updated Application: %v", repoAppl)

	err = repo.DeleteApplication(repoAppl.ID)
	require.NoError(t, err)

	t.Log("Test Application deleted")
}
Example #8
0
func TestPostgres(t *testing.T) {

	// Connect
	repo, err := getPostgresDB()
	require.NoError(t, err)

	emailAddress := fmt.Sprintf("test_%s@%s.com", GetRandomString(5, ""), GetRandomString(5, ""))
	firstName := "neil"
	lastName := "waterman"
	created := time.Now().UTC()
	password := "******"
	// In test have very low complex
	bcryptPassword, err := createHashedPassword(password)
	require.NoError(t, err)

	// Test User functions
	user := User{EmailAddress: emailAddress, FirstName: firstName, LastName: lastName, Password: bcryptPassword, Created: created, Role: RoleAdmin}

	t.Logf("Adding user: %v", user)

	err = repo.SetUser(&user)
	require.NoError(t, err)

	t.Log("User Set")

	repoUser, err := repo.GetUserByEmail(emailAddress)
	require.NoError(t, err)
	require.Equal(t, emailAddress, repoUser.EmailAddress)
	require.Equal(t, firstName, repoUser.FirstName)
	require.Equal(t, lastName, repoUser.LastName)
	require.Equal(t, bcryptPassword, repoUser.Password)
	require.WithinDuration(t, created, repoUser.Created, time.Duration(5*time.Second))
	require.Equal(t, RoleAdmin, repoUser.Role)
	require.True(t, repoUser.ID > 0)

	t.Logf("Got user: %v", repoUser)

	lastName = "waterboy"
	repoUser.LastName = lastName
	err = repo.UpdateUser(repoUser)
	require.NoError(t, err)

	t.Logf("Updated User: %v", repoUser)

	repoUser, err = repo.GetUser(repoUser.ID)
	require.NoError(t, err)
	require.Equal(t, emailAddress, repoUser.EmailAddress)
	require.Equal(t, firstName, repoUser.FirstName)
	require.Equal(t, lastName, repoUser.LastName)
	require.Equal(t, bcryptPassword, repoUser.Password)
	require.WithinDuration(t, created, repoUser.Created, time.Duration(5*time.Second))
	require.Equal(t, RoleAdmin, repoUser.Role)
	require.True(t, repoUser.ID > 0)

	t.Logf("Got by id user: %v", repoUser)

	err = repo.DeleteUser(repoUser.ID)
	require.NoError(t, err)

	t.Log("User deleted")

}