Skip to content

Commit

Permalink
test edge cases and change confirmUser function
Browse files Browse the repository at this point in the history
  • Loading branch information
yaruwangway committed Jul 14, 2021
1 parent 61240fb commit 04c098d
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 79 deletions.
34 changes: 28 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/interchainberlin/slackbot/testserver"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -205,9 +204,13 @@ func Shellout(command string) (error, string, string) {
return err, stdout.String(), stderr.String()
}

<<<<<<< HEAD
// Aadd a slack user to the pooltoy blockchain using the admin user alice and giving the new user non-admin permissions
// wait 5 seconds so that the transactions is completed
// return an error if the transaction failed
=======
// working directory is not the directory keeps the key, the~/.pooltoy/keyring_test is? however, the key info is stored in file with suffix .info rathter than .json
>>>>>>> 199f573 (test edge cases and change confirmUser function)
func createNewUserAccount(user, username string) error {
fmt.Printf("createNewUserAccount(%s, %s)\n", user, username)
username = strings.ReplaceAll(username, " ", "_")
Expand All @@ -221,9 +224,12 @@ func createNewUserAccount(user, username string) error {
return nil
}

<<<<<<< HEAD
// Add a local key representing this slack user.
// Return an error if this fails
// Do not return an error, if the user key already exists
=======
>>>>>>> 199f573 (test edge cases and change confirmUser function)
func createNewUserKey(user, username string) error {
fmt.Printf("createNewUserKey(%s, %s)\n", user, username)
err, out, errout := Shellout(fmt.Sprintf("pooltoy keys add %s --keyring-backend test", user))
Expand Down Expand Up @@ -259,6 +265,7 @@ func confirmUser(user, username string) error {
}

if err != nil {
<<<<<<< HEAD
// there's an error, find out if it's just that the key doesn't exist
if user != "" {
err = createNewUserKey(user, username)
Expand All @@ -276,6 +283,22 @@ func confirmUser(user, username string) error {
fmt.Println("errout", errout)
if err != nil && strings.Index(errout, "Error: rpc error: code = NotFound") != -1 {
return createNewUserAccount(user, username)
=======

return errors.New(fmt.Sprintf("%s is not found", user))

} else {
err, out, errout = Shellout(fmt.Sprintf("pooltoy q bank -o json balances $(pooltoy keys show %s -a --keyring-backend test) | jq \".balances\"", user))
fmt.Println(fmt.Sprintf("pooltoy q bank -o json balances $(pooltoy keys show %s -a --keyring-backend test) | jq \".balances\"", user))
if err!= nil{
fmt.Println("err", err)
fmt.Println("out", out)
fmt.Println("confirmUser errout", errout)
}
if err != nil && strings.Index(errout, "ERROR: unknown address: bank") != -1 {
return createNewUserAccount(user, username)
}
>>>>>>> 199f573 (test edge cases and change confirmUser function)
}
return nil
}
Expand Down Expand Up @@ -336,6 +359,7 @@ func parseTxResult(out string) (map[string]string, error) {
return tx, nil
}

// temporal solution for the cosmos sdk issue #9663, output format issue
func txErr(out string) bool {
txMap, err := parseTxResult(out)
if err != nil {
Expand Down Expand Up @@ -566,7 +590,7 @@ func balance(userid string, text []string, getUserID UserGetter) string {
err = confirmUser(senderID, senderUsername)
if err != nil {
return fmt.Sprintf("ERROR: %s (%s)", err.Error(), userid)
}
}

if len(text) != 1 || text[0] == "" {
return fmt.Sprintf("Sorry %s, I don't understand that command. Please follow the format '/balance [user]'", senderUsername)
Expand All @@ -583,7 +607,6 @@ func balance(userid string, text []string, getUserID UserGetter) string {
if err != nil {
return fmt.Sprintf("ERROR: %s (%s)", err.Error(), userid)
}

command := fmt.Sprintf("pooltoy q bank -o json balances $(pooltoy keys show %s -a --keyring-backend test) | jq \".balances\"", queriedID)
fmt.Printf("Try command '%s\n", command)

Expand Down Expand Up @@ -623,8 +646,6 @@ func balance(userid string, text []string, getUserID UserGetter) string {

func main() {

go testserver.ForwardServer()

http.HandleFunc("/", botHandler)

crt := os.Getenv("LETSENCRYPT_CRT")
Expand All @@ -634,5 +655,6 @@ func main() {
} else {
log.Fatalln(http.ListenAndServeTLS(":"+port, crt, key, nil))
}

}


Loading

0 comments on commit 04c098d

Please sign in to comment.