Skip to content

Commit

Permalink
improve checking for valid year
Browse files Browse the repository at this point in the history
  • Loading branch information
StreakInTheSky committed Feb 11, 2024
1 parent 7138cf9 commit 4a522fd
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 35 deletions.
30 changes: 25 additions & 5 deletions aoc/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
)

const firstYear = 2015
const currentYear = 2023

type httpClient interface {
Do(req *http.Request) (*http.Response, error)
}

var client httpClient = &http.Client{}

func validateURL(inputURL string) error {
// / validates the input URL based on the ETC/UTC-5 date as that is when puzzles are unlocked
func validateURL(inputURL string, now time.Time) error {
parsedURL, err := url.Parse(inputURL)

if err != nil {
Expand All @@ -41,8 +41,9 @@ func validateURL(inputURL string) error {
if err != nil {
return err
}
if year < firstYear || year > currentYear {
return fmt.Errorf("Invalid year: %d", year)

if err := validateYear(year, now); err != nil {
return err
}

if parsedPath[2] != "day" {
Expand All @@ -60,6 +61,19 @@ func validateURL(inputURL string) error {
return nil
}

func validateYear(year int, now time.Time) error {
currentYear := now.Year()
if year < firstYear || year > currentYear {
return fmt.Errorf("Invalid year: %d", year)
}
currentMonth := now.Month()
if year == currentYear && currentMonth != time.December {
return errors.New("It is not December yet")
}

return nil
}

func checkCookie(cookie http.Cookie) error {
if cookie.Name != "session" || cookie.Value == "" {
return errors.New("No session cookie")
Expand Down Expand Up @@ -91,7 +105,13 @@ func makeCookie(sessionID string) (cookie http.Cookie, err error) {

// Fetch fetches input for advent of code url and a user's session cookie
func fetch(url string, cookie http.Cookie) (res *http.Response, err error) {
if err = validateURL(url); err != nil {
est, err := time.LoadLocation("America/New_York")
if err != nil {
return res, err
}

today := time.Now().In(est);
if err = validateURL(url, today); err != nil {
return res, err
}

Expand Down
90 changes: 60 additions & 30 deletions aoc/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,114 +9,144 @@ import (
)

// Tests for Url Validater
const now_string = "2023-01-01 00:01:01" // the date to compare for validation

// Get a time struct from the given today to test against
func getNow(t *testing.T) time.Time {
now, err := time.Parse("2006-01-02 15:04:05", now_string)
if err != nil {
t.Fatal(err)
}

return now
}

func TestValidateUrl(t *testing.T) {
url := "https://adventofcode.com/2022/day/1"
now := getNow(t)

if err := validateURL(url); err != nil {
if err := validateURL(url, now); err != nil {
t.Error(err)
}
}

func TestErrorIfNotUrl(t *testing.T) {
url := "123"
now := getNow(t)

if err := validateURL(url); err == nil {
if err := validateURL(url, now); err == nil {
t.Errorf("%s should not be a valid url", url)
}
}

func TestErrorIfNotAdventOfCode(t *testing.T) {
url := "https://google.com"
now := getNow(t)

if err := validateURL(url); err == nil {
if err := validateURL(url, now); err == nil {
t.Errorf("%s should return an error", url)
}

}

func TestErrorIfNoPath(t *testing.T) {
url := "https://adventofcode.com"
now := getNow(t)

if err := validateURL(url); err == nil {
if err := validateURL(url, now); err == nil {
t.Errorf("%s should return an error", url)
}
}

func TestErrorIfPathTooShort(t *testing.T) {
url := "https://adventofcode.com"
path := "/1/2"
now := getNow(t)

if err := validateURL(fmt.Sprintf("%s%s", url, path)); err == nil {
if err := validateURL(fmt.Sprintf("%s%s", url, path), now); err == nil {
t.Errorf("Should have error because %s is too short", path)
}
}

func TestErrorIfPathTooLong(t *testing.T) {
url := "https://adventofcode.com"
path := "/1/2/3/4"
now := getNow(t)

if err := validateURL(fmt.Sprintf("%s%s", url, path)); err == nil {
if err := validateURL(fmt.Sprintf("%s%s", url, path), now); err == nil {
t.Errorf("Should have error because %s is too long", path)
}
}

func TestErrorIfNoYear(t *testing.T) {
url := "http://adventofcode.com/"
path := "not/a/year"
now := getNow(t)

if err := validateURL(fmt.Sprintf("%s%s", url, path)); err == nil {
if err := validateURL(fmt.Sprintf("%s%s", url, path), now); err == nil {
t.Errorf("Should have error because %s does not have a year", path)
}
}

func TestErrorOnEarlyYear(t *testing.T) {
url := "http://adventofcode.com/"
const year = 2014

if err := validateURL(fmt.Sprintf("%s%d", url, year)); err == nil {
t.Errorf("%d should be an invalid year", year)
}
}

func TestErrorOnLateYear(t *testing.T) {
url := "http://adventofcode.com/"
const year = 2024

if err := validateURL(fmt.Sprintf("%s%d", url, year)); err == nil {
t.Errorf("%d should be an invalid year", year)
}
}

func TestErrorIfDayNotInPath(t *testing.T) {
url := "http://adventofcode.com/2021"
notDay := "/not/1"
now := getNow(t)

if err := validateURL(fmt.Sprintf("%s%s", url, notDay)); err == nil {
if err := validateURL(fmt.Sprintf("%s%s", url, notDay), now); err == nil {
t.Error("Should be an error when no day in url", url)
}
}

// Tests for Day Validation
func TestErrorIfDayTooLow(t *testing.T) {
url := "http://adventofcode.com/2021/day/"
day := 0
now := getNow(t)

if err := validateURL(fmt.Sprintf("%s%d", url, day)); err == nil {
if err := validateURL(fmt.Sprintf("%s%d", url, day), now); err == nil {
t.Errorf("%d should not be a valid day", day)
}
}

func TestErrorIfDayTooHigh(t *testing.T) {
url := "http://adventofcode.com/2021/day/"
day := 26
now := getNow(t)

if err := validateURL(fmt.Sprintf("%s%d", url, day)); err == nil {
if err := validateURL(fmt.Sprintf("%s%d", url, day), now); err == nil {
t.Errorf("%d should not be a valid day", day)
}
}

// Tests for Cookie checker
// Tests for year validation
func TestErrorOnEarlyYear(t *testing.T) {
const year = 2014
now := getNow(t)

if err := validateYear(year, now); err == nil {
t.Errorf("%d should be an invalid year", year)
}
}

func TestErrorOnLateYear(t *testing.T) {
const year = 2024
now := getNow(t)

if err := validateYear(year, now); err == nil {
t.Errorf("%d should be an invalid year", year)
}
}

func TestErrorOnCurrentYearEarlyMonth(t *testing.T) {
const year = 2023
now := getNow(t)

if err := validateYear(year, now); err == nil {
t.Errorf("%d should be an invalid year for the current date, %s", year, now_string)
}
}

// Tests for Cookie checker
func TestValidCookie(t *testing.T) {
cookie := http.Cookie{
Name: "session",
Expand Down

0 comments on commit 4a522fd

Please sign in to comment.