-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathetebase_test.go
120 lines (98 loc) · 2.78 KB
/
etebase_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
// SPDX-FileCopyrightText: © 2020 Etebase Authors
// SPDX-License-Identifier: BSD-3-Clause
package etebase_test
import (
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/suite"
"github.com/etesync/etebase-go"
)
type AccountSuite struct {
suite.Suite
account *etebase.Account
user etebase.User
password string
}
const (
// host, or host:port, on which the test server is running
hostEnv = "ETEBASE_TEST_HOST"
)
func (s *AccountSuite) newClient() *etebase.Client {
host := os.Getenv(hostEnv)
if host == "" {
s.T().Skip("Define " + hostEnv + " to run this test")
}
return etebase.NewClient(etebase.ClientOptions{
Host: host,
Logger: s.T(), // testing.T implements Logf
})
}
// SetupSuite signs-up a new user and makes sure that the account instance is
// pointing to a valid etebase server.
// It run once, before the tests in the suite are run.
func (s *AccountSuite) SetupSuite() {
acc, err := etebase.Signup(s.newClient(), s.user, s.password)
s.Require().NoError(err)
ok, err := acc.IsEtebaseServer()
s.Require().NoError(err)
s.Require().True(ok)
}
// SetupTest logs-in and keep a reference of the account.
// It run before each test.
func (s *AccountSuite) SetupTest() {
acc, err := etebase.Login(s.newClient(), s.user.Username, s.password)
s.Require().NoError(err)
s.account = acc
}
// TestPasswordChange changes the password and tries to login a user using the new password.
// It changes the password to the previous one so that SetupTest can still login.
func (s *AccountSuite) TestPasswordChange() {
newPassword := "a-new-password"
s.Require().NoError(
s.account.PasswordChange(newPassword),
)
_, err := etebase.Login(s.newClient(), s.user.Username, newPassword)
s.Require().NoError(err)
s.Require().NoError(
s.account.PasswordChange(s.password),
)
}
func (s *AccountSuite) TestLogin() {
s.Run("UserNotFound", func() {
_, err := etebase.Login(s.newClient(), "some-not-existing-user", s.password)
s.Require().Error(err)
s.Require().Contains(err.Error(), "not found")
})
s.Run("WrongPassword", func() {
_, err := etebase.Login(s.newClient(), s.user.Username, "wrong-password")
s.Require().Error(err)
s.Require().Contains(err.Error(), "Wrong password")
})
}
// TestLogout logs-out an account twice. The second time it shouldn't be
// authorized.
func (s *AccountSuite) TestLogout() {
s.Require().NoError(
s.account.Logout(),
)
err := s.account.Logout()
s.Require().Error(err)
s.Require().Contains(err.Error(), "Invalid token.")
}
func TestEtebaseSuite(t *testing.T) {
var (
id = fmt.Sprintf("%d", time.Now().Unix())
username = "test-user-" + id
user = etebase.User{
Username: username,
Email: username + "@test.com",
}
password = "secret"
)
suite.Run(t, &AccountSuite{
user: user,
password: password,
})
}