Browse Source

added config store to database

Matthew R Kasun 4 years ago
parent
commit
e4744a6279
2 changed files with 33 additions and 12 deletions
  1. 22 4
      test/api_test.go
  2. 11 8
      test/group_test.go

+ 22 - 4
test/api_test.go

@@ -2,6 +2,7 @@ package main
 
 import (
 	"bytes"
+	"context"
 	"encoding/json"
 	"io/ioutil"
 	"net/http"
@@ -50,6 +51,19 @@ func TestMain(m *testing.M) {
 	var waitgroup sync.WaitGroup
 	waitgroup.Add(1)
 	go controller.HandleRESTRequests(&waitgroup)
+	var gconf models.GlobalConfig
+	gconf.ServerGRPC = "localhost:8081"
+	gconf.PortGRPC = "50051"
+	//err := SetGlobalConfig(gconf)
+	collection := mongoconn.Client.Database("netmaker").Collection("config")
+	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+	defer cancel()
+	//create, _, err := functions.GetGlobalConfig()
+	_, err := collection.InsertOne(ctx, gconf)
+	if err != nil {
+		panic("could not create config store")
+	}
+
 	//wait for http server to start
 	time.Sleep(time.Second * 1)
 	os.Exit(m.Run())
@@ -178,11 +192,15 @@ func networkExists(t *testing.T) bool {
 	assert.Equal(t, http.StatusOK, response.StatusCode)
 	err = json.NewDecoder(response.Body).Decode(&Networks)
 	assert.Nil(t, err, err)
-	if Networks == nil {
-		return false
-	} else {
-		return true
+	for i, network := range Networks {
+		t.Log(i, network)
+		if network.NetID == "" {
+			return false
+		} else {
+			return true
+		}
 	}
+	return false
 }
 
 func deleteNetworks(t *testing.T) {

+ 11 - 8
test/group_test.go

@@ -115,7 +115,7 @@ func TestGetNetwork(t *testing.T) {
 	})
 }
 
-func TestDeletenetwork(t *testing.T) {
+func TestDeleteMetwork(t *testing.T) {
 
 	t.Run("InvalidKey", func(t *testing.T) {
 		response, err := api(t, "", http.MethodDelete, baseURL+"/api/networks/skynet", "badkey")
@@ -154,11 +154,10 @@ func TestDeletenetwork(t *testing.T) {
 	})
 }
 
-func TestCreateAccessKey(t *testing.T) {
-
-	if !networkExists(t) {
-		createNetwork(t)
-	}
+func TestCreateKey(t *testing.T) {
+	//ensure we are working with known networks
+	deleteNetworks(t)
+	createNetwork(t)
 
 	key := models.AccessKey{}
 	key.Name = "skynet"
@@ -222,6 +221,9 @@ func TestCreateAccessKey(t *testing.T) {
 }
 
 func TestDeleteKey(t *testing.T) {
+	//ensure we are working with known networks
+	deleteNetworks(t)
+	createNetwork(t)
 	//ensure key exists
 	createKey(t)
 	t.Run("KeyValid", func(t *testing.T) {
@@ -273,7 +275,9 @@ func TestDeleteKey(t *testing.T) {
 }
 
 func TestGetKeys(t *testing.T) {
-
+	//ensure we are working with known networks
+	deleteNetworks(t)
+	createNetwork(t)
 	createKey(t)
 	t.Run("Valid", func(t *testing.T) {
 		response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/skynet/keys", "secretkey")
@@ -284,7 +288,6 @@ func TestGetKeys(t *testing.T) {
 		err = json.NewDecoder(response.Body).Decode(&keys)
 		assert.Nil(t, err, err)
 	})
-	//deletekeys
 	t.Run("Invalidnetwork", func(t *testing.T) {
 		response, err := api(t, "", http.MethodGet, baseURL+"/api/networks/badnetwork/keys", "secretkey")
 		assert.Nil(t, err, err)