tokenStore_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package login
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. . "github.com/onsi/gomega"
  9. "github.com/stretchr/testify/suite"
  10. )
  11. type tokenStoreTestSuite struct {
  12. suite.Suite
  13. }
  14. func (suite *tokenStoreTestSuite) TestCreateStoreFromExistingFolder() {
  15. existingDir, err := ioutil.TempDir("", "test_store")
  16. Expect(err).To(BeNil())
  17. storePath := filepath.Join(existingDir, tokenStoreFilename)
  18. store, err := newTokenStore(storePath)
  19. Expect(err).To(BeNil())
  20. Expect((store.filePath)).To(Equal(storePath))
  21. }
  22. func (suite *tokenStoreTestSuite) TestCreateStoreFromNonExistingFolder() {
  23. existingDir, err := ioutil.TempDir("", "test_store")
  24. Expect(err).To(BeNil())
  25. storePath := filepath.Join(existingDir, "new", tokenStoreFilename)
  26. store, err := newTokenStore(storePath)
  27. Expect(err).To(BeNil())
  28. Expect((store.filePath)).To(Equal(storePath))
  29. newDir, err := os.Stat(filepath.Join(existingDir, "new"))
  30. Expect(err).To(BeNil())
  31. Expect(newDir.Mode().IsDir()).To(BeTrue())
  32. }
  33. func (suite *tokenStoreTestSuite) TestErrorIfParentFolderIsAFile() {
  34. existingDir, err := ioutil.TempFile("", "test_store")
  35. Expect(err).To(BeNil())
  36. storePath := filepath.Join(existingDir.Name(), tokenStoreFilename)
  37. _, err = newTokenStore(storePath)
  38. Expect(err).To(MatchError(errors.New("cannot use path " + storePath + " ; " + existingDir.Name() + " already exists and is not a directory")))
  39. }
  40. func TestTokenStoreSuite(t *testing.T) {
  41. RegisterTestingT(t)
  42. suite.Run(t, new(tokenStoreTestSuite))
  43. }