forked from cs3org/reva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
195 lines (164 loc) · 4.69 KB
/
json_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2018-2023 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package json
import (
"context"
"os"
"path/filepath"
"testing"
"time"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
invitepb "github.com/cs3org/go-cs3apis/cs3/ocm/invite/v1beta1"
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/cs3org/reva/v2/pkg/ocm/invite"
"github.com/stretchr/testify/assert"
)
func setupTestManager(t *testing.T) *manager {
// Create a temporary file for the test
tmpDir, err := os.MkdirTemp("", "invite-manager-test")
if err != nil {
t.Fatalf("error creating temp file: %v", err)
}
// Initialize a new manager with the temp file
config := map[string]interface{}{
"file": filepath.Join(tmpDir, "ocm-invites.json"),
}
mgr, err := New(config)
if err != nil {
t.Fatalf("error initializing invite manager: %v", err)
}
return mgr.(*manager)
}
func TestAddToken(t *testing.T) {
mgr := setupTestManager(t)
ctx := context.Background()
// Test data
token := &invitepb.InviteToken{
Token: "test-token",
UserId: &userpb.UserId{
OpaqueId: "user1",
},
Expiration: &typespb.Timestamp{
Seconds: uint64(time.Now().Add(24 * time.Hour).Unix()),
},
}
// Add token
err := mgr.AddToken(ctx, token)
assert.NoError(t, err)
// Check if the token was added correctly
storedToken, err := mgr.GetToken(ctx, token.Token)
assert.NoError(t, err)
assert.Equal(t, token, storedToken)
}
func TestGetToken_NotFound(t *testing.T) {
mgr := setupTestManager(t)
ctx := context.Background()
// Try to get a non-existent token
_, err := mgr.GetToken(ctx, "non-existent-token")
assert.ErrorIs(t, err, invite.ErrTokenNotFound)
}
func TestListTokens(t *testing.T) {
mgr := setupTestManager(t)
ctx := context.Background()
initiator := &userpb.UserId{
OpaqueId: "user1",
}
// Add some tokens
token1 := &invitepb.InviteToken{
Token: "token1",
UserId: initiator,
}
token2 := &invitepb.InviteToken{
Token: "token2",
UserId: initiator,
}
_ = mgr.AddToken(ctx, token1)
_ = mgr.AddToken(ctx, token2)
// List tokens
tokens, err := mgr.ListTokens(ctx, initiator)
assert.NoError(t, err)
assert.Len(t, tokens, 2)
}
func TestAddRemoteUser(t *testing.T) {
mgr := setupTestManager(t)
ctx := context.Background()
initiator := &userpb.UserId{
OpaqueId: "user1",
}
remoteUser := &userpb.User{
Id: &userpb.UserId{
OpaqueId: "remoteUser1",
},
}
// Add remote user
err := mgr.AddRemoteUser(ctx, initiator, remoteUser)
assert.NoError(t, err)
// Retrieve remote user and verify
storedUser, err := mgr.GetRemoteUser(ctx, initiator, remoteUser.Id)
assert.NoError(t, err)
assert.Equal(t, remoteUser, storedUser)
}
func TestGetRemoteUser_NotFound(t *testing.T) {
mgr := setupTestManager(t)
ctx := context.Background()
initiator := &userpb.UserId{
OpaqueId: "user1",
}
// Try to get a non-existent remote user
_, err := mgr.GetRemoteUser(ctx, initiator, &userpb.UserId{OpaqueId: "non-existent"})
assert.Error(t, err)
}
func TestDeleteRemoteUser(t *testing.T) {
mgr := setupTestManager(t)
ctx := context.Background()
initiator := &userpb.UserId{
OpaqueId: "user1",
}
remoteUser1 := &userpb.User{
Id: &userpb.UserId{
OpaqueId: "remoteUser1",
},
}
remoteUser2 := &userpb.User{
Id: &userpb.UserId{
OpaqueId: "remoteUser2",
},
}
remoteUser3 := &userpb.User{
Id: &userpb.UserId{
OpaqueId: "remoteUser3",
},
}
// Add remote users
err := mgr.AddRemoteUser(ctx, initiator, remoteUser1)
assert.NoError(t, err)
err = mgr.AddRemoteUser(ctx, initiator, remoteUser2)
assert.NoError(t, err)
err = mgr.AddRemoteUser(ctx, initiator, remoteUser3)
assert.NoError(t, err)
// Delete remote user
err = mgr.DeleteRemoteUser(ctx, initiator, remoteUser2.Id)
assert.NoError(t, err)
// Try to get the deleted user
_, err = mgr.GetRemoteUser(ctx, initiator, remoteUser1.Id)
assert.NoError(t, err)
_, err = mgr.GetRemoteUser(ctx, initiator, remoteUser2.Id)
assert.Error(t, err)
_, err = mgr.GetRemoteUser(ctx, initiator, remoteUser3.Id)
assert.NoError(t, err)
}