forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_test.go
92 lines (75 loc) · 2.16 KB
/
user_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
package tfe
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestUsersReadCurrent(t *testing.T) {
client := testClient(t)
ctx := context.Background()
u, err := client.Users.ReadCurrent(ctx)
assert.NoError(t, err)
assert.NotEmpty(t, u.ID)
assert.NotEmpty(t, u.AvatarURL)
assert.NotEmpty(t, u.Username)
t.Run("two factor options are decoded", func(t *testing.T) {
assert.NotNil(t, u.TwoFactor)
})
}
func TestUsersUpdate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
uTest, err := client.Users.ReadCurrent(ctx)
require.NoError(t, err)
// Make sure we reset the current user when we're done.
defer func() {
_, err := client.Users.Update(ctx, UserUpdateOptions{
Email: String(uTest.Email),
Username: String(uTest.Username),
})
if err != nil {
t.Logf("Error updating user: %s", err)
}
}()
t.Run("without any options", func(t *testing.T) {
_, err := client.Users.Update(ctx, UserUpdateOptions{})
require.NoError(t, err)
u, err := client.Users.ReadCurrent(ctx)
assert.NoError(t, err)
assert.Equal(t, u, uTest)
})
t.Run("with a new username", func(t *testing.T) {
_, err := client.Users.Update(ctx, UserUpdateOptions{
Username: String("NewTestUsername"),
})
require.NoError(t, err)
u, err := client.Users.ReadCurrent(ctx)
assert.NoError(t, err)
assert.Equal(t, "NewTestUsername", u.Username)
})
t.Run("with a new email address", func(t *testing.T) {
_, err := client.Users.Update(ctx, UserUpdateOptions{
Email: String("[email protected]"),
})
require.NoError(t, err)
u, err := client.Users.ReadCurrent(ctx)
email := ""
if u.UnconfirmedEmail != "" {
email = u.UnconfirmedEmail
} else if u.Email != "" {
email = u.Email
} else {
t.Fatalf("cannot test with user %q because both email and unconfirmed email are empty", u.ID)
}
assert.NoError(t, err)
assert.Equal(t, "[email protected]", email)
})
t.Run("with invalid email address", func(t *testing.T) {
u, err := client.Users.Update(ctx, UserUpdateOptions{
Email: String("notamailaddress"),
})
assert.Nil(t, u)
assert.Error(t, err)
})
}