Skip to content
Permalink
Browse files

new tests

  • Loading branch information
stremovsky committed Feb 13, 2020
1 parent cdbcb71 commit 21446d37e904c446f79548b0c672c71b95ad0eea
Showing with 41 additions and 64 deletions.
  1. +2 −0 src/bunker_test.go
  2. +18 −50 src/userapps_test.go
  3. +21 −14 src/xtokens_test.go
@@ -89,6 +89,8 @@ func init() {
}
db.initUserApps()
var cfg Config
cfg.Sms.TwilioToken = "ttoken"
cfg.SelfService.AppRecordChange = []string{"*"}
e := mainEnv{db, cfg, make(chan struct{})}
rootToken, err = db.createRootXtoken()
if err != nil {
@@ -1,85 +1,53 @@
package main

import (
"encoding/json"
"fmt"
"net/http/httptest"
"strings"
"testing"
)

func helpCreateUserApp(userTOKEN string, appName string, appJSON string) (map[string]interface{}, error) {
request := httptest.NewRequest("POST", "http://localhost:3000/v1/userapp/token/"+userTOKEN+"/"+appName, strings.NewReader(appJSON))
rr := httptest.NewRecorder()
url := "http://localhost:3000/v1/userapp/token/" + userTOKEN + "/" + appName
request := httptest.NewRequest("POST", url, strings.NewReader(appJSON))
request.Header.Set("Content-Type", "application/json")
request.Header.Set("X-Bunker-Token", rootToken)

router.ServeHTTP(rr, request)
var raw map[string]interface{}
fmt.Printf("Got: %s\n", rr.Body.Bytes())
err := json.Unmarshal(rr.Body.Bytes(), &raw)
return raw, err
return helpServe(request)
}

func helpUpdateUserApp(userTOKEN string, appName string, appJSON string) (map[string]interface{}, error) {
request := httptest.NewRequest("PUT", "http://localhost:3000/v1/userapp/token/"+userTOKEN+"/"+appName, strings.NewReader(appJSON))
rr := httptest.NewRecorder()
url := "http://localhost:3000/v1/userapp/token/" + userTOKEN + "/" + appName
request := httptest.NewRequest("PUT", url, strings.NewReader(appJSON))
request.Header.Set("Content-Type", "application/json")
request.Header.Set("X-Bunker-Token", rootToken)

router.ServeHTTP(rr, request)
var raw map[string]interface{}
fmt.Printf("Got: %s\n", rr.Body.Bytes())
err := json.Unmarshal(rr.Body.Bytes(), &raw)
return raw, err
return helpServe(request)
}

func helpGetUserApp(userTOKEN string, appName string) (map[string]interface{}, error) {
request := httptest.NewRequest("GET", "http://localhost:3000/v1/userapp/token/"+userTOKEN+"/"+appName, nil)
rr := httptest.NewRecorder()
url := "http://localhost:3000/v1/userapp/token/" + userTOKEN + "/" + appName
request := httptest.NewRequest("GET", url, nil)
request.Header.Set("X-Bunker-Token", rootToken)

router.ServeHTTP(rr, request)
var raw map[string]interface{}
fmt.Printf("Got: %s\n", rr.Body.Bytes())
err := json.Unmarshal(rr.Body.Bytes(), &raw)
return raw, err
return helpServe(request)
}

func helpDeleteUserApp(userTOKEN string, appName string) (map[string]interface{}, error) {
request := httptest.NewRequest("DELETE", "http://localhost:3000/v1/userapp/token/"+userTOKEN+"/"+appName, nil)
rr := httptest.NewRecorder()
url := "http://localhost:3000/v1/userapp/token/" + userTOKEN + "/" + appName
request := httptest.NewRequest("DELETE", url, nil)
request.Header.Set("X-Bunker-Token", rootToken)

router.ServeHTTP(rr, request)
var raw map[string]interface{}
fmt.Printf("Got: %s\n", rr.Body.Bytes())
err := json.Unmarshal(rr.Body.Bytes(), &raw)
return raw, err
return helpServe(request)
}

func helpGetUserAppList(userTOKEN string) (map[string]interface{}, error) {
request := httptest.NewRequest("GET", "http://localhost:3000/v1/userapp/token/"+userTOKEN, nil)
rr := httptest.NewRecorder()
url := "http://localhost:3000/v1/userapp/token/" + userTOKEN
request := httptest.NewRequest("GET", url, nil)
request.Header.Set("X-Bunker-Token", rootToken)

router.ServeHTTP(rr, request)
var raw map[string]interface{}
fmt.Printf("Got: %s\n", rr.Body.Bytes())
err := json.Unmarshal(rr.Body.Bytes(), &raw)
return raw, err
return helpServe(request)
}

func helpGetAppList() (map[string]interface{}, error) {
request := httptest.NewRequest("GET", "http://localhost:3000/v1/userapps", nil)
rr := httptest.NewRecorder()
url := "http://localhost:3000/v1/userapps"
request := httptest.NewRequest("GET", url, nil)
request.Header.Set("X-Bunker-Token", rootToken)

router.ServeHTTP(rr, request)
var raw map[string]interface{}
fmt.Printf("Got: %s\n", rr.Body.Bytes())
err := json.Unmarshal(rr.Body.Bytes(), &raw)
return raw, err
return helpServe(request)
}

func TestCreateUserApp(t *testing.T) {
@@ -67,9 +67,8 @@ func TestUserLoginDelete(t *testing.T) {
}
}
userTOKEN := raw["token"].(string)
raw2, _ := helpCreateUserLogin("email", email)
status = raw2["status"].(string)
if status == "error" {
raw, _ = helpCreateUserLogin("email", email)
if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" {
t.Fatalf("Failed to create user login: %s", raw["message"].(string))
}
/*
@@ -82,27 +81,35 @@ func TestUserLoginDelete(t *testing.T) {
tmpCode = userBson["tempcode"].(int32)
}
*/
raw3, _ := helpCreateUserLoginEnter("email", email, "4444") //strconv.Itoa(int(tmpCode)))
if raw3["status"].(string) == "error" {
t.Fatalf("Failed to create user login: %s", raw3["message"].(string))
raw, _ = helpCreateUserLoginEnter("email", email, "4444") //strconv.Itoa(int(tmpCode)))
if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" {
t.Fatalf("Failed to create user login: %s", raw["message"].(string))
}
xtoken := raw3["xtoken"].(string)
xtoken := raw["xtoken"].(string)
fmt.Printf("User login *** xtoken: %s\n", xtoken)
oldRootToken := rootToken
rootToken = xtoken
raw4, _ := helpGetUserAppList(userTOKEN)
if raw4["status"].(string) == "error" {
raw, _ = helpCreateUserApp(userTOKEN, "testapp", `{"custom":1}`)
if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" {
t.Fatalf("Failed to create app: testapp")
}
raw, _ = helpUpdateUserApp(userTOKEN, "testapp", `{"custom2":"abc"}`)
if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" {
t.Fatalf("Failed to update app: testapp")
}
raw, _ = helpGetUserAppList(userTOKEN)
if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" {
t.Fatalf("Failed to get user app list with user xtoken\n")
}
fmt.Printf("apps: %s\n", raw4["apps"])
fmt.Printf("apps: %s\n", raw["apps"])
// user asks to forget-me
raw5, _ := helpDeleteUser("token", userTOKEN)
if raw5["status"].(string) != "ok" {
raw, _ = helpDeleteUser("token", userTOKEN)
if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" {
t.Fatalf("Failed to delete user")
}
if raw5["result"].(string) != "request-created" {
if raw["result"].(string) != "request-created" {
}
rtoken0 := raw5["rtoken"].(string)
rtoken0 := raw["rtoken"].(string)
rootToken = oldRootToken
// get user requests
raw6, _ := helpGetUserRequests()

0 comments on commit 21446d3

Please sign in to comment.
You can’t perform that action at this time.