|
| 1 | +// Copyright 2018-2023 CERN |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// In applying this license, CERN does not waive the privileges and immunities |
| 16 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 17 | +// or submit itself to any jurisdiction. |
| 18 | + |
| 19 | +package json |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | + |
| 28 | + userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" |
| 29 | + invitepb "github.com/cs3org/go-cs3apis/cs3/ocm/invite/v1beta1" |
| 30 | + typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" |
| 31 | + "github.com/cs3org/reva/v2/pkg/ocm/invite" |
| 32 | + "github.com/stretchr/testify/assert" |
| 33 | +) |
| 34 | + |
| 35 | +func setupTestManager(t *testing.T) *manager { |
| 36 | + // Create a temporary file for the test |
| 37 | + tmpDir, err := os.MkdirTemp("", "invite-manager-test") |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("error creating temp file: %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + // Initialize a new manager with the temp file |
| 43 | + config := map[string]interface{}{ |
| 44 | + "file": filepath.Join(tmpDir, "ocm-invites.json"), |
| 45 | + } |
| 46 | + mgr, err := New(config) |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("error initializing invite manager: %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + return mgr.(*manager) |
| 52 | +} |
| 53 | + |
| 54 | +func TestAddToken(t *testing.T) { |
| 55 | + mgr := setupTestManager(t) |
| 56 | + ctx := context.Background() |
| 57 | + |
| 58 | + // Test data |
| 59 | + token := &invitepb.InviteToken{ |
| 60 | + Token: "test-token", |
| 61 | + UserId: &userpb.UserId{ |
| 62 | + OpaqueId: "user1", |
| 63 | + }, |
| 64 | + Expiration: &typespb.Timestamp{ |
| 65 | + Seconds: uint64(time.Now().Add(24 * time.Hour).Unix()), |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + // Add token |
| 70 | + err := mgr.AddToken(ctx, token) |
| 71 | + assert.NoError(t, err) |
| 72 | + |
| 73 | + // Check if the token was added correctly |
| 74 | + storedToken, err := mgr.GetToken(ctx, token.Token) |
| 75 | + assert.NoError(t, err) |
| 76 | + assert.Equal(t, token, storedToken) |
| 77 | +} |
| 78 | + |
| 79 | +func TestGetToken_NotFound(t *testing.T) { |
| 80 | + mgr := setupTestManager(t) |
| 81 | + ctx := context.Background() |
| 82 | + |
| 83 | + // Try to get a non-existent token |
| 84 | + _, err := mgr.GetToken(ctx, "non-existent-token") |
| 85 | + assert.ErrorIs(t, err, invite.ErrTokenNotFound) |
| 86 | +} |
| 87 | + |
| 88 | +func TestListTokens(t *testing.T) { |
| 89 | + mgr := setupTestManager(t) |
| 90 | + ctx := context.Background() |
| 91 | + |
| 92 | + initiator := &userpb.UserId{ |
| 93 | + OpaqueId: "user1", |
| 94 | + } |
| 95 | + |
| 96 | + // Add some tokens |
| 97 | + token1 := &invitepb.InviteToken{ |
| 98 | + Token: "token1", |
| 99 | + UserId: initiator, |
| 100 | + } |
| 101 | + token2 := &invitepb.InviteToken{ |
| 102 | + Token: "token2", |
| 103 | + UserId: initiator, |
| 104 | + } |
| 105 | + |
| 106 | + _ = mgr.AddToken(ctx, token1) |
| 107 | + _ = mgr.AddToken(ctx, token2) |
| 108 | + |
| 109 | + // List tokens |
| 110 | + tokens, err := mgr.ListTokens(ctx, initiator) |
| 111 | + assert.NoError(t, err) |
| 112 | + assert.Len(t, tokens, 2) |
| 113 | +} |
| 114 | + |
| 115 | +func TestAddRemoteUser(t *testing.T) { |
| 116 | + mgr := setupTestManager(t) |
| 117 | + ctx := context.Background() |
| 118 | + |
| 119 | + initiator := &userpb.UserId{ |
| 120 | + OpaqueId: "user1", |
| 121 | + } |
| 122 | + |
| 123 | + remoteUser := &userpb.User{ |
| 124 | + Id: &userpb.UserId{ |
| 125 | + OpaqueId: "remoteUser1", |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + // Add remote user |
| 130 | + err := mgr.AddRemoteUser(ctx, initiator, remoteUser) |
| 131 | + assert.NoError(t, err) |
| 132 | + |
| 133 | + // Retrieve remote user and verify |
| 134 | + storedUser, err := mgr.GetRemoteUser(ctx, initiator, remoteUser.Id) |
| 135 | + assert.NoError(t, err) |
| 136 | + assert.Equal(t, remoteUser, storedUser) |
| 137 | +} |
| 138 | + |
| 139 | +func TestGetRemoteUser_NotFound(t *testing.T) { |
| 140 | + mgr := setupTestManager(t) |
| 141 | + ctx := context.Background() |
| 142 | + |
| 143 | + initiator := &userpb.UserId{ |
| 144 | + OpaqueId: "user1", |
| 145 | + } |
| 146 | + |
| 147 | + // Try to get a non-existent remote user |
| 148 | + _, err := mgr.GetRemoteUser(ctx, initiator, &userpb.UserId{OpaqueId: "non-existent"}) |
| 149 | + assert.Error(t, err) |
| 150 | +} |
| 151 | + |
| 152 | +func TestDeleteRemoteUser(t *testing.T) { |
| 153 | + mgr := setupTestManager(t) |
| 154 | + ctx := context.Background() |
| 155 | + |
| 156 | + initiator := &userpb.UserId{ |
| 157 | + OpaqueId: "user1", |
| 158 | + } |
| 159 | + |
| 160 | + remoteUser1 := &userpb.User{ |
| 161 | + Id: &userpb.UserId{ |
| 162 | + OpaqueId: "remoteUser1", |
| 163 | + }, |
| 164 | + } |
| 165 | + remoteUser2 := &userpb.User{ |
| 166 | + Id: &userpb.UserId{ |
| 167 | + OpaqueId: "remoteUser2", |
| 168 | + }, |
| 169 | + } |
| 170 | + remoteUser3 := &userpb.User{ |
| 171 | + Id: &userpb.UserId{ |
| 172 | + OpaqueId: "remoteUser3", |
| 173 | + }, |
| 174 | + } |
| 175 | + |
| 176 | + // Add remote users |
| 177 | + err := mgr.AddRemoteUser(ctx, initiator, remoteUser1) |
| 178 | + assert.NoError(t, err) |
| 179 | + err = mgr.AddRemoteUser(ctx, initiator, remoteUser2) |
| 180 | + assert.NoError(t, err) |
| 181 | + err = mgr.AddRemoteUser(ctx, initiator, remoteUser3) |
| 182 | + assert.NoError(t, err) |
| 183 | + |
| 184 | + // Delete remote user |
| 185 | + err = mgr.DeleteRemoteUser(ctx, initiator, remoteUser2.Id) |
| 186 | + assert.NoError(t, err) |
| 187 | + |
| 188 | + // Try to get the deleted user |
| 189 | + _, err = mgr.GetRemoteUser(ctx, initiator, remoteUser1.Id) |
| 190 | + assert.NoError(t, err) |
| 191 | + _, err = mgr.GetRemoteUser(ctx, initiator, remoteUser2.Id) |
| 192 | + assert.Error(t, err) |
| 193 | + _, err = mgr.GetRemoteUser(ctx, initiator, remoteUser3.Id) |
| 194 | + assert.NoError(t, err) |
| 195 | +} |
0 commit comments