Commit ecb83ff7 by Liza

Add http package; have auth0 test delete user it has just registered after test is done

parent ca1998b6
......@@ -2,11 +2,9 @@ package auth0
import (
"fmt"
"strings"
"net/http"
"io/ioutil"
"encoding/json"
"errors"
http2 "gitlab.com/drakonka/gosnaillife/lib/infrastructure/http"
)
......@@ -31,27 +29,16 @@ func (ap *Auth0) Register(email, password string) (id string, err error) {
"password":"%s",
"connection":"Username-Password-Authentication"}`, ap.clientId, email, password)
payload := strings.NewReader(postSpec)
req, err := http.NewRequest("POST", url, payload)
if err != nil {
return id, err
}
req.Header.Add("content-type", "application/json")
res, err := http.DefaultClient.Do(req)
body, err := http2.PostAndGetResBody(url, postSpec)
if err != nil {
return id, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return id, err
}
var data map[string]interface{}
err = json.Unmarshal([]byte(string(body)), &data)
if val, ok := data["_id"]; ok {
id = val.(string)
id = "auth0|" + val.(string)
}
return id, err
}
......@@ -64,20 +51,7 @@ func (ap *Auth0) Login(username, password string) (err error) {
"client_secret": "%s",
"client_id":"%s"}`, username, password, ap.clientSecret, ap.clientId)
payload := strings.NewReader(postSpec)
req, err := http.NewRequest("POST", url, payload)
if err != nil {
return err
}
req.Header.Add("content-type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
body, err := http2.PostAndGetResBody(url, postSpec)
if err != nil {
return err
}
......
......@@ -5,15 +5,22 @@ import (
"gitlab.com/drakonka/gosnaillife/lib/infrastructure/auth/cli/auth0"
"fmt"
"os"
"gitlab.com/drakonka/gosnaillife/lib/infrastructure/http"
"encoding/json"
"errors"
)
var creds testCredentials
var userId string
func TestMain(m *testing.M) {
creds = GetCredentials()
ret := m.Run()
if userId != "" {
deleteTestUser()
}
creds = testCredentials{}
os.Exit(ret)
}
......@@ -26,6 +33,8 @@ func TestAuth0Signup(t *testing.T) {
t.Errorf("Signup test failed: ", err.Error())
} else if id == "0" {
t.Errorf("Signup test failed; unexpected user ID: " + string(id))
} else {
userId = id
}
}
......@@ -48,4 +57,55 @@ func getProvider(creds testCredentials) *auth0.Auth0 {
configCreds["domain"] = creds.domain
provider.Configure(configCreds)
return &provider
}
func deleteTestUser() {
token, err := getManagementAccessToken()
if err != nil {
fmt.Println(err.Error())
return
}
deleteUser(token)
}
func deleteUser(token string) {
urlSuffix := fmt.Sprintf("api/v2/users/%s", userId)
url := creds.domain + urlSuffix
authHeader := fmt.Sprintf("Bearer %s", token)
body, err := http.Delete(url, authHeader)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(body))
}
func getManagementAccessToken() (token string, err error){
url := creds.domain + "oauth/token"
audienceUrl := creds.domain + "api/v2/"
fmt.Println("AUDIENCE URL")
fmt.Println(audienceUrl)
postSpec := fmt.Sprintf( `{"client_id":"%s",
"client_secret":"%s",
"audience":"%s",
"grant_type":"client_credentials"}`, creds.clientId, creds.clientSecret, audienceUrl)
body, err := http.PostAndGetResBody(url, postSpec)
if err != nil {
fmt.Println(err.Error())
return token, err
}
var data map[string]interface{}
err = json.Unmarshal([]byte(string(body)), &data)
if e, ok := data["error"]; ok {
msg := fmt.Sprintf("%s - %s", e, data["error_description"])
err = errors.New(msg)
return token, err
}
if val, ok := data["access_token"]; ok {
token = val.(string)
}
return token, err
}
\ No newline at end of file
......@@ -2,6 +2,6 @@ package auth
type Provider interface {
Configure(credentials map[string]string)
Register(email, password string) (id int, err error)
Login(username, password string)
Register(email, password string) (id string, err error)
Login(username, password string) (err error)
}
\ No newline at end of file
package http
import (
"net/http"
"strings"
"io/ioutil"
)
func PostAndGetResBody(url, spec string) (body []byte, err error) {
payload := strings.NewReader(spec)
req, err := http.NewRequest("POST", url, payload)
if err != nil {
return body, err
}
req.Header.Add("content-type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return body, err
}
defer res.Body.Close()
body, err = ioutil.ReadAll(res.Body)
return body, err
}
func Delete(url, authHeader string) (body []byte, err error) {
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return body, err
}
req.Header.Set("Authorization", authHeader)
req.Header.Add("content-type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return body, err
}
defer res.Body.Close()
body, err = ioutil.ReadAll(res.Body)
return body, err
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment