add mock auth server
diff --git a/change_test.go b/change_test.go index 4a1981b..09ef3dd 100644 --- a/change_test.go +++ b/change_test.go
@@ -166,7 +166,7 @@ return nil } -func (t *dummyTokenManager) getToken() *oauthToken { +func (t *dummyTokenManager) getToken() *OauthToken { return nil }
diff --git a/data.go b/data.go index 8e7cca6..3bd5591 100644 --- a/data.go +++ b/data.go
@@ -453,7 +453,7 @@ // first start - no row, generate a UUID and store it err = nil newInstanceID = true - info.InstanceID = generateUUID() + info.InstanceID = GenerateUUID() log.Debugf("Inserting new apid instance id %s", info.InstanceID) db.Exec("INSERT INTO APID (instance_id, apid_cluster_id, last_snapshot_info) VALUES (?,?,?)", @@ -463,7 +463,7 @@ log.Debug("Detected apid cluster id change in config. Apid will start clean") err = nil newInstanceID = true - info.InstanceID = generateUUID() + info.InstanceID = GenerateUUID() db.Exec("REPLACE INTO APID (instance_id, apid_cluster_id, last_snapshot_info) VALUES (?,?,?)", info.InstanceID, info.ClusterID, "") @@ -501,7 +501,7 @@ */ //TODO: Change to https://tools.ietf.org/html/rfc4122 based implementation such as https://github.com/google/uuid -func generateUUID() string { +func GenerateUUID() string { buff := make([]byte, 16) numRead, err := rand.Read(buff)
diff --git a/dockertests/docker_test.go b/dockertests/docker_test.go index af4a111..7092e1a 100644 --- a/dockertests/docker_test.go +++ b/dockertests/docker_test.go
@@ -1,13 +1,16 @@ package dockertests import ( - "github.com/30x/apidApigeeSync" + _ "github.com/30x/apidApigeeSync" "github.com/30x/apid-core" . "github.com/onsi/ginkgo" - "net/http/httptest" + . "github.com/onsi/gomega" "os" "github.com/30x/apid-core/factory" - "github.com/Sirupsen/logrus" + "testing" + "fmt" + "time" + "net/http/httptest" ) const ( @@ -43,39 +46,34 @@ * This test suite acts like a dummy plugin. It listens to events emitted by * apidApigeeSync and runs tests. */ +var _ = BeforeSuite(func() { + hostname := os.Getenv("APIGEE_SYNC_DOCKER_IP") + os.Setenv("APID_CONFIG_FILE", "./apid_config.yaml") + + fmt.Println("Run BeforeSuite") + + apid.Initialize(factory.DefaultServicesFactory()) + config = apid.Config() + + // Auth Server + config.Set(configName, "dockerIT") + config.Set(configConsumerKey, "dummyKey") + config.Set(configConsumerSecret, "dummySecret") + config.Set(configApidClusterId, "testClusterId") + //testServer := initDummyAuthServer() + + // Setup dependencies + config.Set(configChangeServerBaseURI, hostname+":"+dockerCsPort) + config.Set(configSnapServerBaseURI, hostname+":"+dockerSsPort) + //config.Set(configProxyServerBaseURI, testServer.URL) + + // init plugin + apid.RegisterPlugin(initPlugin) + apid.InitializePlugins("dockerTest") +}) var _ = Describe("dockerIT", func() { - - - BeforeSuite(func() { - hostname := os.Getenv("APIGEE_SYNC_DOCKER_IP") - - apid.Initialize(factory.DefaultServicesFactory()) - config = apid.Config() - - // Set log level - config.Set(configLogLevel, logrus.DebugLevel.String()) - - // Auth Server - config.Set(configName, "dockerIT") - config.Set(configConsumerKey, "dummyKey") - config.Set(configConsumerSecret, "dummySecret") - config.Set(configApidClusterId, "testClusterId") - testServer := initDummyAuthServer() - - // Setup dependencies - config.Set(configChangeServerBaseURI, hostname+":"+dockerCsPort) - config.Set(configSnapServerBaseURI, hostname+":"+dockerSsPort) - config.Set(configProxyServerBaseURI, testServer.URL) - - // init plugin - apid.RegisterPlugin(initPlugin) - apid.InitializePlugins("dockerTest") - }) - - - Context("Generic Replication", func() { var _ = BeforeEach(func() { @@ -85,24 +83,18 @@ log.Debug("CS: " + config.GetString(configChangeServerBaseURI)) log.Debug("SS: " + config.GetString(configSnapServerBaseURI)) log.Debug("Auth: " + config.GetString(configProxyServerBaseURI)) - }) + + time.Sleep(5 * time.Second) + Expect(1).To(Equal(1)) + }, 30) }) }) func initDummyAuthServer() (testServer *httptest.Server) { testRouter := apid.API().Router() testServer = httptest.NewServer(testRouter) - // set up mock server - mockParms := apidApigeeSync.MockParms{ - ReliableAPI: true, - ClusterID: config.GetString(configApidClusterId), - TokenKey: config.GetString(configConsumerKey), - TokenSecret: config.GetString(configConsumerSecret), - Scope: "dockerTest", - Organization: "dockerTest", - Environment: "prod", - } - apidApigeeSync.Mock(mockParms, testRouter) + mockAuthServer := &MockAuthServer{} + mockAuthServer.Start(testRouter) return } @@ -122,4 +114,9 @@ log.Info(pluginName + " initialized.") return pluginData, nil +} + +func TestDockerApigeeSync(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "ApigeeSync Docker Suite") } \ No newline at end of file
diff --git a/dockertests/mockAuthServer.go b/dockertests/mockAuthServer.go new file mode 100644 index 0000000..9d79fd6 --- /dev/null +++ b/dockertests/mockAuthServer.go
@@ -0,0 +1,33 @@ +package dockertests + + +import ( + "github.com/30x/apidApigeeSync" + "github.com/30x/apid-core" + "encoding/json" + "net/http" +) + +const oauthExpiresIn = 2 * 60 + +type MockAuthServer struct { + +} + +func (m *MockAuthServer) sendToken (w http.ResponseWriter, req *http.Request) { + oauthToken := apidApigeeSync.GenerateUUID() + res := apidApigeeSync.OauthToken{ + AccessToken: oauthToken, + ExpiresIn: oauthExpiresIn, + } + body, err := json.Marshal(res) + if err != nil { + panic(err) + } + w.Write(body) +} + +func (m *MockAuthServer) Start (router apid.Router) { + router.HandleFunc("/accesstoken", m.sendToken).Methods("POST") +} +
diff --git a/managerInterfaces.go b/managerInterfaces.go index ae80cec..5022bdd 100644 --- a/managerInterfaces.go +++ b/managerInterfaces.go
@@ -22,7 +22,7 @@ type tokenManager interface { getBearerToken() string invalidateToken() error - getToken() *oauthToken + getToken() *OauthToken close() getRetrieveNewTokenClosure(*url.URL) func(chan bool) error start()
diff --git a/mock_server.go b/mock_server.go index 4ce28bf..94a0651 100644 --- a/mock_server.go +++ b/mock_server.go
@@ -248,8 +248,8 @@ err = json.Unmarshal(plInfo, &plugInfo) Expect(err).NotTo(HaveOccurred()) - m.oauthToken = generateUUID() - res := oauthToken{ + m.oauthToken = GenerateUUID() + res := OauthToken{ AccessToken: m.oauthToken, ExpiresIn: oauthExpiresIn, } @@ -266,7 +266,7 @@ Expect(scopes).To(ContainElement(m.params.ClusterID)) - w.Header().Set("Transicator-Snapshot-TXID", generateUUID()) + w.Header().Set("Transicator-Snapshot-TXID", GenerateUUID()) if len(scopes) == 1 { //send bootstrap db @@ -405,7 +405,7 @@ func (m *MockServer) createDeployment() tableRowMap { deploymentID := m.nextDeploymentID() - bundleID := generateUUID() + bundleID := GenerateUUID() listen := apid.Config().GetString("api_listen") _, port, err := net.SplitHostPort(listen)
diff --git a/token.go b/token.go index 0b7521e..1612025 100644 --- a/token.go +++ b/token.go
@@ -47,7 +47,7 @@ closed: make(chan bool), getTokenChan: make(chan bool), invalidateTokenChan: make(chan bool), - returnTokenChan: make(chan *oauthToken), + returnTokenChan: make(chan *OauthToken), invalidateDone: make(chan bool), isClosed: &isClosedInt, } @@ -55,14 +55,14 @@ } type simpleTokenManager struct { - token *oauthToken + token *OauthToken isClosed *int32 quitPollingForToken chan bool closed chan bool getTokenChan chan bool invalidateTokenChan chan bool refreshTimer <-chan time.Time - returnTokenChan chan *oauthToken + returnTokenChan chan *OauthToken invalidateDone chan bool } @@ -109,7 +109,7 @@ return nil } -func (t *simpleTokenManager) getToken() *oauthToken { +func (t *simpleTokenManager) getToken() *OauthToken { //has been closed if atomic.LoadInt32(t.isClosed) == int32(1) { log.Debug("TokenManager: getToken() called on closed tokenManager") @@ -190,7 +190,7 @@ return expected200Error{} } - var token oauthToken + var token OauthToken err = json.Unmarshal(body, &token) if err != nil { log.Errorf("unable to unmarshal JSON response '%s': %v", string(body), err) @@ -222,7 +222,7 @@ } } -type oauthToken struct { +type OauthToken struct { IssuedAt int64 `json:"issuedAt"` AppName string `json:"applicationName"` Scope string `json:"scope"` @@ -241,21 +241,21 @@ var noTime time.Time -func (t *oauthToken) isValid() bool { +func (t *OauthToken) isValid() bool { if t == nil || t.AccessToken == "" { return false } return t.AccessToken != "" && time.Now().Before(t.ExpiresAt) } -func (t *oauthToken) refreshIn() time.Duration { +func (t *OauthToken) refreshIn() time.Duration { if t == nil || t.ExpiresAt == noTime { return time.Duration(0) } return t.ExpiresAt.Sub(time.Now()) - refreshFloatTime } -func (t *oauthToken) needsRefresh() bool { +func (t *OauthToken) needsRefresh() bool { if t == nil || t.ExpiresAt == noTime { return true }
diff --git a/token_test.go b/token_test.go index 472f790..85b71c7 100644 --- a/token_test.go +++ b/token_test.go
@@ -36,7 +36,7 @@ It("should calculate valid token", func() { log.Info("Starting token tests...") - t := &oauthToken{ + t := &OauthToken{ AccessToken: "x", ExpiresIn: 120, ExpiresAt: time.Now().Add(2 * time.Minute), @@ -48,7 +48,7 @@ It("should calculate expired token", func() { - t := &oauthToken{ + t := &OauthToken{ AccessToken: "x", ExpiresIn: 0, ExpiresAt: time.Now(), @@ -60,7 +60,7 @@ It("should calculate token needing refresh", func() { - t := &oauthToken{ + t := &OauthToken{ AccessToken: "x", ExpiresIn: 59, ExpiresAt: time.Now().Add(time.Minute - time.Second), @@ -72,7 +72,7 @@ It("should calculate on empty token", func() { - t := &oauthToken{} + t := &OauthToken{} Expect(t.refreshIn().Seconds()).To(BeNumerically("<=", 0)) Expect(t.needsRefresh()).To(BeTrue()) Expect(t.isValid()).To(BeFalse()) @@ -85,7 +85,7 @@ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer GinkgoRecover() - res := oauthToken{ + res := OauthToken{ AccessToken: "ABCD", ExpiresIn: 1, } @@ -113,8 +113,8 @@ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer GinkgoRecover() - res := oauthToken{ - AccessToken: generateUUID(), + res := OauthToken{ + AccessToken: GenerateUUID(), ExpiresIn: 1, } body, err := json.Marshal(res) @@ -153,7 +153,7 @@ finished <- true } - res := oauthToken{ + res := OauthToken{ AccessToken: string(count), ExpiresIn: 1, } @@ -194,7 +194,7 @@ Expect(r.Header.Get("updated_at_apid")).NotTo(BeEmpty()) finished <- true } - res := oauthToken{ + res := OauthToken{ AccessToken: string(count), ExpiresIn: 200, }