-
Notifications
You must be signed in to change notification settings - Fork 30
/
e2e_upload_folder_test.go
181 lines (158 loc) · 3.33 KB
/
e2e_upload_folder_test.go
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//go:build e2e
// +build e2e
package cmdupload
import (
"context"
"errors"
"fmt"
"immich-go/immich"
"immich-go/logger"
"testing"
"time"
"github.com/joho/godotenv"
)
func TestE2eUpload(t *testing.T) {
var myEnv map[string]string
myEnv, err := godotenv.Read("../.env")
if err != nil {
t.Errorf("cant initialize environment variables: %s", err)
return
}
host := myEnv["IMMICH_HOST"]
if host == "" {
host = "http://localhost:2283"
}
key := myEnv["IMMICH_KEY"]
if key == "" {
t.Fatal("you must provide the IMMICH's API KEY in the environnement variable DEBUG_IMMICH_KEY")
}
tests := []struct {
name string
args []string
resetImmich bool
expectError bool
}{
{
name: "upload google photos",
args: []string{
"-google-photos",
"../../test-data/low_high/Takeout",
},
resetImmich: true,
expectError: false,
},
{
name: "upload folder",
args: []string{
"../../test-data/low_high/high",
},
// resetImmich: true,
expectError: false,
},
{
name: "upload folder",
args: []string{
"../../test-data/low_high/high",
},
// resetImmich: true,
expectError: false,
},
{
name: "upload folder *.jpg",
args: []string{
"-google-photos",
"../../test-data/test_folder/*.jpg",
},
resetImmich: true,
expectError: true,
},
{
name: "upload folder *.jpg",
args: []string{
"../../test-data/test_folder/*/*.jpg",
},
// resetImmich: true,
expectError: false,
},
{
name: "upload folder *.jpg - dry run",
args: []string{
"-dry-run",
"../../test-data/full_takeout (copy)/Takeout/Google Photos/Photos from 2023",
},
// resetImmich: true,
expectError: false,
},
}
logger := logger.NewLogger(logger.Debug, true, false)
ic, err := immich.NewImmichClient(host, key)
if err != nil {
t.Error(err)
return
}
ctx := context.Background()
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if tc.resetImmich {
err := resetImmich(ic, "debug@example.com")
if err != nil {
t.Error(err)
return
}
}
err = UploadCommand(ctx, ic, logger, tc.args)
if (tc.expectError && err == nil) || (!tc.expectError && err != nil) {
t.Errorf("unexpected err: %v", err)
return
}
})
}
}
// ResetImmich
// ⛔: will remove the content of the server.‼️
// Give the user of the connection to confirm the server instance: debug@example.com
//
func resetImmich(ic *immich.ImmichClient, user string) error {
u, err := ic.ValidateConnection(context.Background())
if err != nil {
return err
}
if u.Email != user {
return fmt.Errorf("Not the debug server")
}
albums, err := ic.GetAllAlbums(context.Background())
if err != nil {
return err
}
for _, album := range albums {
err = ic.DeleteAlbum(context.Background(), album.ID)
if err != nil {
return err
}
}
assets, err := ic.GetAllAssets(context.Background(), nil)
if err != nil {
return err
}
ids := []string{}
for _, a := range assets {
ids = append(ids, a.ID)
}
err = ic.DeleteAssets(context.Background(), ids, true)
if err != nil {
return nil
}
attempts := 5
for attempts > 0 {
assets, err := ic.GetAllAssets(context.Background(), nil)
if err != nil {
return err
}
if len(assets) == 0 {
return nil
}
time.Sleep(5 * time.Second)
attempts--
}
return errors.New("can't reset immich")
}