Skip to content

Commit

Permalink
add tests for upload of guest files and guest link creation where Fil…
Browse files Browse the repository at this point in the history
…eLifetime is not never
  • Loading branch information
Balk-Z committed Dec 8, 2024
1 parent 2a8cbc1 commit a0f9147
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
34 changes: 34 additions & 0 deletions handlers/guest_links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,40 @@ func TestGuestLinksPostAcceptsValidRequest(t *testing.T) {
MaxFileUploads: makeGuestUploadCountLimit(1),
},
},
{
description: "guest file expires in 1 day",
payload: `{
"label": "For my good pal, Maurice",
"urlExpirationTime":"2030-01-02T03:04:25Z",
"fileLifetime":"24h0m0s",
"maxFileBytes": 1048576,
"maxFileUploads": 1
}`,
expected: picoshare.GuestLink{
Label: picoshare.GuestLinkLabel("For my good pal, Maurice"),
UrlExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
FileLifetime: picoshare.NewFileLifetimeInDays(1),
MaxFileBytes: makeGuestUploadMaxFileBytes(1048576),
MaxFileUploads: makeGuestUploadCountLimit(1),
},
},
{
description: "guest file expires in 30 day",
payload: `{
"label": "For my good pal, Maurice",
"urlExpirationTime":"2030-01-02T03:04:25Z",
"fileLifetime":"720h0m0s",
"maxFileBytes": 1048576,
"maxFileUploads": 1
}`,
expected: picoshare.GuestLink{
Label: picoshare.GuestLinkLabel("For my good pal, Maurice"),
UrlExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
FileLifetime: picoshare.NewFileLifetimeInDays(30),
MaxFileBytes: makeGuestUploadMaxFileBytes(1048576),
MaxFileUploads: makeGuestUploadCountLimit(1),
},
},
} {
t.Run(tt.description, func(t *testing.T) {
dataStore := test_sqlite.New()
Expand Down
2 changes: 1 addition & 1 deletion handlers/templates/pages/guest-link-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ <h1 class="title">Guest Links</h1>
{{ formatExpiration .UrlExpires }}
</td>
<td class="is-vcentered expiration">
{{ formatFileExpiration .FileLifetime }}
{{ .FileLifetime.FriendlyName }}
</td>
<td class="is-vcentered">
{{ formatSizeLimit .MaxFileBytes }}
Expand Down
44 changes: 38 additions & 6 deletions handlers/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"encoding/json"
"io"
"math"
"mime/multipart"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -280,9 +281,10 @@ func TestGuestUpload(t *testing.T) {
{
description: "valid upload to guest link",
guestLinkInStore: picoshare.GuestLink{
ID: picoshare.GuestLinkID("abcdefgh23456789"),
Created: mustParseTime("2022-01-01T00:00:00Z"),
UrlExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
ID: picoshare.GuestLinkID("abcdefgh23456789"),
Created: mustParseTime("2022-01-01T00:00:00Z"),
UrlExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
FileLifetime: picoshare.FileLifetimeInfinite,
},
guestLinkID: "abcdefgh23456789",
status: http.StatusOK,
Expand Down Expand Up @@ -378,6 +380,28 @@ func TestGuestUpload(t *testing.T) {
guestLinkID: "abcdefgh23456789",
status: http.StatusBadRequest,
},
{
description: "guest file expires in 1 day",
guestLinkInStore: picoshare.GuestLink{
ID: picoshare.GuestLinkID("abcdefgh23456789"),
Created: mustParseTime("2022-01-01T00:00:00Z"),
UrlExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
FileLifetime: picoshare.NewFileLifetimeInDays(1),
},
guestLinkID: "abcdefgh23456789",
status: http.StatusOK,
},
{
description: "guest file expires in 365 day",
guestLinkInStore: picoshare.GuestLink{
ID: picoshare.GuestLinkID("abcdefgh23456789"),
Created: mustParseTime("2022-01-01T00:00:00Z"),
UrlExpires: mustParseExpirationTime("2030-01-02T03:04:25Z"),
FileLifetime: picoshare.NewFileLifetimeInDays(365),
},
guestLinkID: "abcdefgh23456789",
status: http.StatusOK,
},
} {
t.Run(tt.description, func(t *testing.T) {
store := test_sqlite.New()
Expand Down Expand Up @@ -441,13 +465,21 @@ func TestGuestUpload(t *testing.T) {
}

// Guest uploads never expire.
// if got, want := entry.Expires, picoshare.NeverExpire; got != want {
// t.Errorf("expiration=%v, want=%v", got, want)
// }
if got, want := convertExpirationTimeToFileLifetime(entry.Expires), tt.guestLinkInStore.FileLifetime; got != want {
t.Errorf("expiration=%v, want=%v", got, want)
}
})
}
}

func convertExpirationTimeToFileLifetime(et picoshare.ExpirationTime) picoshare.FileLifetime {
if et == picoshare.NeverExpire {
return picoshare.FileLifetimeInfinite
}
delta := math.Round(time.Until(time.Time(et)).Hours() / 24)
return picoshare.NewFileLifetimeInDays(uint16(delta))
}

func createMultipartFormBody(filename, note string, r io.Reader) (io.Reader, string) {
var b bytes.Buffer
bw := bufio.NewWriter(&b)
Expand Down
11 changes: 0 additions & 11 deletions handlers/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,6 @@ func (s Server) guestLinkIndexGet() http.HandlerFunc {
}
return fmt.Sprintf("%s (%.0f days%s)", t.Format(time.DateOnly), math.Abs(delta.Hours())/24, suffix)
},
"formatFileExpiration": func(flt picoshare.FileLifetime) string {
if flt == picoshare.FileLifetimeInfinite {
return "Never"
}

letterS := "s"
if flt.Duration().Hours() < 25 {
letterS = ""
}
return fmt.Sprintf("After %.0f day%s", math.Abs(flt.Duration().Hours()/24), letterS)
},
"isActive": func(gl picoshare.GuestLink) bool {
return gl.IsActive()
},
Expand Down

0 comments on commit a0f9147

Please sign in to comment.