1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
package web
import (
"database/sql"
"net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"strings"
"testing"
"time"
"github.com/martinlehoux/biking_home/config"
"github.com/martinlehoux/biking_home/rides"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newWebTestServer(t *testing.T) (*Server, *sql.DB) {
t.Helper()
db, err := sql.Open("sqlite3", ":memory:")
require.NoError(t, err)
t.Cleanup(func() { db.Close() })
_, err = db.Exec(`
create table rides (
id integer primary key,
external_id text unique not null,
gpx_path text not null,
name text not null,
type text not null,
start_date text not null,
distance_m real not null,
moving_time_s integer not null,
elapsed_time_s integer not null,
total_elevation_gain_m real not null,
average_speed_mps real not null,
created_at text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
updated_at text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now'))
)
`)
require.NoError(t, err)
configPath := filepath.Join(t.TempDir(), "config.yaml")
appConfig := config.Default()
appConfig.Strava.ClientID = "123"
appConfig.Strava.ClientSecret = "secret"
require.NoError(t, config.Save(configPath, appConfig))
return NewServer(db, configPath), db
}
func TestHandlerRendersRidesPage(t *testing.T) {
server, db := newWebTestServer(t)
require.NoError(t, rides.Save(db, rides.Ride{
ExternalID: "strava:1234",
GPXPath: "missing.gpx",
Name: "Long Ride",
Type: "Ride",
StartDate: time.Date(2026, 8, 1, 7, 0, 0, 0, time.UTC),
DistanceM: 20_000,
}))
require.NoError(t, rides.Save(db, rides.Ride{
ExternalID: "strava:5678",
GPXPath: "short.gpx",
Name: "Short Ride",
Type: "Ride",
StartDate: time.Date(2026, 8, 2, 7, 0, 0, 0, time.UTC),
DistanceM: 9_999,
}))
req := httptest.NewRequest(http.MethodGet, "/", nil)
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, req)
assert.Equal(t, http.StatusOK, response.Code)
assert.Contains(t, response.Body.String(), "All rides")
assert.Contains(t, response.Body.String(), "Long Ride")
assert.NotContains(t, response.Body.String(), "Short Ride")
assert.Contains(t, response.Body.String(), "Cotacol")
assert.Contains(t, response.Body.String(), "Cotacol / 100 km")
}
func TestSyncPageRequestsAuthorization(t *testing.T) {
server, _ := newWebTestServer(t)
req := httptest.NewRequest(http.MethodGet, "/sync", nil)
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, req)
assert.Equal(t, http.StatusOK, response.Code)
assert.Contains(t, response.Body.String(), "Authorize Strava")
}
func TestSyncRedirectsToOAuthWhenUnauthenticated(t *testing.T) {
server, _ := newWebTestServer(t)
form := url.Values{"from": {"2026-08-01"}, "to": {"2026-08-04"}}
req := httptest.NewRequest(http.MethodPost, "/sync", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, req)
assert.Equal(t, http.StatusFound, response.Code)
location, err := url.Parse(response.Header().Get("Location"))
require.NoError(t, err)
assert.Equal(t, "/strava/login", location.Path)
assert.Equal(t, "/sync?from=2026-08-01&to=2026-08-04", location.Query().Get("return_to"))
}
func TestStravaLoginRedirectsToAuthorize(t *testing.T) {
server, _ := newWebTestServer(t)
req := httptest.NewRequest(http.MethodGet, "/strava/login?return_to=/sync", nil)
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, req)
assert.Equal(t, http.StatusFound, response.Code)
location, err := url.Parse(response.Header().Get("Location"))
require.NoError(t, err)
assert.Equal(t, "www.strava.com", location.Host)
assert.Equal(t, "/oauth/authorize", location.Path)
assert.Equal(t, "activity:read_all", location.Query().Get("scope"))
assert.Equal(t, "http://localhost:8080/strava/callback", location.Query().Get("redirect_uri"))
assert.NotEmpty(t, location.Query().Get("state"))
}
func TestParseDateRangeMakesEndInclusive(t *testing.T) {
from, to, err := parseDateRange("2026-08-01", "2026-08-04")
require.NoError(t, err)
assert.Equal(t, "2026-08-01T00:00:00Z", from.Format("2006-01-02T15:04:05Z07:00"))
assert.Equal(t, "2026-08-05T00:00:00Z", to.Format("2006-01-02T15:04:05Z07:00"))
}
func TestFormatCotacolPer100Km(t *testing.T) {
assert.Equal(t, "20.0", formatCotacolPer100Km(2, 10_000))
assert.Equal(t, "-", formatCotacolPer100Km(2, 0))
}
|