-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Stephen Asbury
committed
Aug 27, 2019
1 parent
db5dd68
commit 533368e
Showing
6 changed files
with
200 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2019 The NATS Authors | ||
* 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. | ||
* | ||
*/ | ||
|
||
package store | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/nats-io/nkeys" | ||
nsc "github.com/nats-io/nsc/cmd/store" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// NKeyFactory is an extensible way to create a keypair | ||
type NKeyFactory func() (nkeys.KeyPair, error) | ||
|
||
// CreateAccountKey makes an account key pair | ||
func CreateAccountKey(t *testing.T) (seed []byte, pub string, kp nkeys.KeyPair) { | ||
return CreateTestNKey(t, nkeys.CreateAccount) | ||
} | ||
|
||
// CreateOperatorKey makes an operator key pair | ||
func CreateOperatorKey(t *testing.T) (seed []byte, pub string, kp nkeys.KeyPair) { | ||
return CreateTestNKey(t, nkeys.CreateOperator) | ||
} | ||
|
||
// CreateTestNKey makes a key pair from a factory | ||
func CreateTestNKey(t *testing.T, f NKeyFactory) ([]byte, string, nkeys.KeyPair) { | ||
kp, err := f() | ||
require.NoError(t, err) | ||
|
||
seed, err := kp.Seed() | ||
require.NoError(t, err) | ||
|
||
pub, err := kp.PublicKey() | ||
require.NoError(t, err) | ||
|
||
return seed, pub, kp | ||
} | ||
|
||
// MakeTempStore builds a temp dir with the store structure | ||
func MakeTempStore(t *testing.T, name string, kp nkeys.KeyPair) *nsc.Store { | ||
p, err := ioutil.TempDir("", "store_test") | ||
require.NoError(t, err) | ||
|
||
var nk *nsc.NamedKey | ||
if kp != nil { | ||
nk = &nsc.NamedKey{Name: name, KP: kp} | ||
} | ||
|
||
s, err := nsc.CreateStore(name, p, nk) | ||
require.NoError(t, err) | ||
require.NotNil(t, s) | ||
return s | ||
} | ||
|
||
// CreateTestStoreForOperator creates a valid nsc folder for testing | ||
func CreateTestStoreForOperator(t *testing.T, name string, operator nkeys.KeyPair) *nsc.Store { | ||
s := MakeTempStore(t, name, operator) | ||
|
||
require.NotNil(t, s) | ||
require.FileExists(t, filepath.Join(s.Dir, ".nsc")) | ||
require.True(t, s.Has("", ".nsc")) | ||
|
||
if operator != nil { | ||
tokenName := fmt.Sprintf("%s.jwt", nsc.SafeName(name)) | ||
require.FileExists(t, filepath.Join(s.Dir, tokenName)) | ||
require.True(t, s.Has("", tokenName)) | ||
} | ||
|
||
return s | ||
} |