token_store_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. Copyright 2020 Docker, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package login
  14. import (
  15. "errors"
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "testing"
  20. . "github.com/onsi/gomega"
  21. "github.com/stretchr/testify/suite"
  22. )
  23. type tokenStoreTestSuite struct {
  24. suite.Suite
  25. }
  26. func (suite *tokenStoreTestSuite) TestCreateStoreFromExistingFolder() {
  27. existingDir, err := ioutil.TempDir("", "test_store")
  28. Expect(err).To(BeNil())
  29. storePath := filepath.Join(existingDir, tokenStoreFilename)
  30. store, err := newTokenStore(storePath)
  31. Expect(err).To(BeNil())
  32. Expect((store.filePath)).To(Equal(storePath))
  33. }
  34. func (suite *tokenStoreTestSuite) TestCreateStoreFromNonExistingFolder() {
  35. existingDir, err := ioutil.TempDir("", "test_store")
  36. Expect(err).To(BeNil())
  37. storePath := filepath.Join(existingDir, "new", tokenStoreFilename)
  38. store, err := newTokenStore(storePath)
  39. Expect(err).To(BeNil())
  40. Expect((store.filePath)).To(Equal(storePath))
  41. newDir, err := os.Stat(filepath.Join(existingDir, "new"))
  42. Expect(err).To(BeNil())
  43. Expect(newDir.Mode().IsDir()).To(BeTrue())
  44. }
  45. func (suite *tokenStoreTestSuite) TestErrorIfParentFolderIsAFile() {
  46. existingDir, err := ioutil.TempFile("", "test_store")
  47. Expect(err).To(BeNil())
  48. storePath := filepath.Join(existingDir.Name(), tokenStoreFilename)
  49. _, err = newTokenStore(storePath)
  50. Expect(err).To(MatchError(errors.New("cannot use path " + storePath + " ; " + existingDir.Name() + " already exists and is not a directory")))
  51. }
  52. func TestTokenStoreSuite(t *testing.T) {
  53. RegisterTestingT(t)
  54. suite.Run(t, new(tokenStoreTestSuite))
  55. }