auth0_test.go 2.66 KB
Newer Older
Liza's avatar
Liza committed
1 2 3 4 5 6 7
package tests

import (
	"testing"
	"gitlab.com/drakonka/gosnaillife/lib/infrastructure/auth/cli/auth0"
	"fmt"
	"os"
Liza's avatar
Liza committed
8 9 10
	"gitlab.com/drakonka/gosnaillife/lib/infrastructure/http"
	"encoding/json"
	"errors"
Liza's avatar
Liza committed
11 12 13
)

var creds testCredentials
Liza's avatar
Liza committed
14
var userId string
Liza's avatar
Liza committed
15 16 17 18

func TestMain(m *testing.M) {
	creds = GetCredentials()
	ret := m.Run()
Liza's avatar
Liza committed
19 20 21
	if userId != "" {
		deleteTestUser()
	}
Liza's avatar
Liza committed
22
	creds = testCredentials{}
Liza's avatar
Liza committed
23

Liza's avatar
Liza committed
24 25 26 27 28 29 30 31 32 33 34 35
	os.Exit(ret)
}


func TestAuth0Signup(t *testing.T) {
	fmt.Printf("Creating user with ID: %s, pass: %s", creds.email, creds.password)
	provider := getProvider(creds)
	id, err := provider.Register(creds.email, creds.password)
	if err != nil {
		t.Errorf("Signup test failed: ", err.Error())
	} else if id == "0" {
		t.Errorf("Signup test failed; unexpected user ID: " + string(id))
Liza's avatar
Liza committed
36 37
	} else {
		userId = id
Liza's avatar
Liza committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	}
}

func TestAuth0Login(t *testing.T) {
	fmt.Printf("Logging in with ID: %s, pass: %s", creds.email, creds.password)
	provider := getProvider(creds)
	err := provider.Login(creds.email, creds.password)
	if err != nil {
		t.Errorf("Login test failed: " + err.Error())
	} else if (provider.HasAccessToken() == false) {
		t.Errorf("Login test failed: Access token not found")
	}
}

func getProvider(creds testCredentials) *auth0.Auth0 {
	provider := auth0.Auth0{}
	configCreds := map[string]string{}
	configCreds["client_id"] = creds.clientId
	configCreds["client_secret"] = creds.clientSecret
	configCreds["domain"] = creds.domain
	provider.Configure(configCreds)
	return &provider
Liza's avatar
Liza committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
}

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
Liza's avatar
Liza committed
111
}