testing profile image

This commit is contained in:
Roland Osborne 2022-02-09 21:39:48 -08:00
parent ba7d1b37a9
commit 1ceefbc50b
4 changed files with 95 additions and 33 deletions

View File

@ -0,0 +1,33 @@
package databag
import (
"errors"
"net/http"
"encoding/base64"
)
func GetProfileImage(w http.ResponseWriter, r *http.Request) {
var data []byte
account, code, err := BearerAppToken(r, true);
if err != nil {
ErrResponse(w, code, err)
return
}
if account.AccountDetail.Image == "" {
ErrResponse(w, http.StatusNotFound, errors.New("profile image not set"))
return
}
data, err = base64.StdEncoding.DecodeString(account.AccountDetail.Image)
if err != nil {
ErrResponse(w, http.StatusNotFound, errors.New("profile image not valid"))
return
}
w.Header().Set("Content-Type", http.DetectContentType(data))
w.Write(data);
}

View File

@ -1,21 +0,0 @@
/*
* DataBag
*
* DataBag provides storage for decentralized identity based self-hosting apps. It is intended to support sharing of personal data and hosting group conversations.
*
* API version: 0.0.1
* Contact: roland.osborne@gmail.com
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
*/
package databag
import (
"net/http"
)
func GetProfileImage(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}

View File

@ -1,6 +1,7 @@
package databag
import (
"io/ioutil"
"errors"
"strings"
"strconv"
@ -52,7 +53,45 @@ func GetTestRevision(status chan *Revision) (rev *Revision) {
}
}
func SendEndpointTest(
func ApiTestData(
endpoint func(http.ResponseWriter, *http.Request),
requestType string,
name string,
params *map[string]string,
body interface{},
tokenType string,
token string,
responseHeader *map[string][]string,
) (data []byte, err error) {
var r *http.Request
var w *httptest.ResponseRecorder
if r, w, err = NewRequest(requestType, name, body); err != nil {
return
}
if params != nil {
r = mux.SetURLVars(r, *params)
}
if token != "" {
r.Header.Add("TokenType", tokenType)
SetBearerAuth(r, token)
}
endpoint(w, r)
resp := w.Result()
if resp.StatusCode != 200 {
err = errors.New("response failed");
return
}
if responseHeader != nil {
*responseHeader = resp.Header
}
data, err = ioutil.ReadAll(resp.Body)
return
}
func ApiTestMsg(
endpoint func(http.ResponseWriter, *http.Request),
requestType string,
name string,

View File

@ -1,7 +1,9 @@
package databag
import (
"bytes"
"testing"
"encoding/base64"
"github.com/stretchr/testify/assert"
)
@ -14,6 +16,8 @@ func TestUpdateProfile(t *testing.T) {
var cProfileRev int64
var cCardRev int64
var profile Profile
var data []byte
var img []byte
// setup testing group
set, err := AddTestGroup("updateprofile")
@ -27,12 +31,12 @@ func TestUpdateProfile(t *testing.T) {
bCardRev = GetTestRevision(set.B.Revisions).Card
cCardRev = GetTestRevision(set.C.Revisions).Card
param["cardId"] = set.B.A.CardId
assert.NoError(t, SendEndpointTest(GetCard, "GET", "/contact/cards/{cardId}",
assert.NoError(t, ApiTestMsg(GetCard, "GET", "/contact/cards/{cardId}",
&param, nil,
APP_TOKENAPP, set.B.Token, &card, nil))
bProfileRev = card.Data.NotifiedProfile
param["cardId"] = set.C.A.CardId
assert.NoError(t, SendEndpointTest(GetCard, "GET", "/contact/cards/{cardId}",
assert.NoError(t, ApiTestMsg(GetCard, "GET", "/contact/cards/{cardId}",
&param, nil,
APP_TOKENAPP, set.C.Token, &card, nil))
cProfileRev = card.Data.NotifiedProfile
@ -43,7 +47,7 @@ func TestUpdateProfile(t *testing.T) {
Location: "San Francisco",
Description: "databaggerr",
};
assert.NoError(t, SendEndpointTest(SetProfile, "PUT", "/profile/data",
assert.NoError(t, ApiTestMsg(SetProfile, "PUT", "/profile/data",
nil, profileData,
APP_TOKENAPP, set.A.Token, &profile, nil))
assert.Equal(t, "databaggerr", profile.Description)
@ -54,7 +58,7 @@ func TestUpdateProfile(t *testing.T) {
// check B notified
param["cardId"] = set.B.A.CardId
assert.NoError(t, SendEndpointTest(GetCard, "GET", "/contact/cards/{cardId}",
assert.NoError(t, ApiTestMsg(GetCard, "GET", "/contact/cards/{cardId}",
&param, nil,
APP_TOKENAPP, set.B.Token, &card, nil))
assert.NotEqual(t, bProfileRev, card.Data.NotifiedProfile)
@ -62,17 +66,17 @@ func TestUpdateProfile(t *testing.T) {
// check C notified
param["cardId"] = set.C.A.CardId
assert.NoError(t, SendEndpointTest(GetCard, "GET", "/contact/cards/{cardId}",
assert.NoError(t, ApiTestMsg(GetCard, "GET", "/contact/cards/{cardId}",
&param, nil,
APP_TOKENAPP, set.C.Token, &card, nil))
assert.NotEqual(t, cProfileRev, card.Data.NotifiedProfile)
assert.NotEqual(t, card.Data.ProfileRevision, card.Data.NotifiedProfile)
// sync profile
assert.NoError(t, SendEndpointTest(GetProfileMessage, "GET", "/profile/message",
assert.NoError(t, ApiTestMsg(GetProfileMessage, "GET", "/profile/message",
nil, nil,
APP_TOKENCONTACT, set.B.A.Token, &msg, nil))
assert.NoError(t, SendEndpointTest(AddCard, "POST", "/contact/cards",
assert.NoError(t, ApiTestMsg(AddCard, "POST", "/contact/cards",
nil, &msg,
APP_TOKENAPP, set.B.Token, &card, nil))
assert.Equal(t, card.Id, set.B.A.CardId)
@ -80,10 +84,10 @@ func TestUpdateProfile(t *testing.T) {
assert.Equal(t, card.Data.CardProfile.Name, "Namer")
// sync profile
assert.NoError(t, SendEndpointTest(GetProfileMessage, "GET", "/profile/message",
assert.NoError(t, ApiTestMsg(GetProfileMessage, "GET", "/profile/message",
nil, nil,
APP_TOKENCONTACT, set.C.A.Token, &msg, nil))
assert.NoError(t, SendEndpointTest(AddCard, "POST", "/contact/cards",
assert.NoError(t, ApiTestMsg(AddCard, "POST", "/contact/cards",
nil, &msg,
APP_TOKENAPP, set.C.Token, &card, nil))
assert.Equal(t, card.Id, set.C.A.CardId)
@ -92,10 +96,17 @@ func TestUpdateProfile(t *testing.T) {
// set profile image
image := "iVBORw0KGgoAAAANSUhEUgAAAaQAAAGkCAIAAADxLsZiAAAFzElEQVR4nOzWUY3jMBhG0e0qSEqoaIqiaEIoGAxh3gZAldid3nMI+JOiXP3bGOMfwLf7v3oAwAxiBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQliByS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ABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBhWz2Az/Laj9UT4BIuOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgITbGGP1BoDLueyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7ICEnwAAAP//DQ4epwV6rzkAAAAASUVORK5CYII="
assert.NoError(t, SendEndpointTest(SetProfileImage, "PUT", "/profile/image",
assert.NoError(t, ApiTestMsg(SetProfileImage, "PUT", "/profile/image",
nil, image,
APP_TOKENAPP, set.A.Token, &profile, nil))
// TODO retrieve and validate profile image
// retrieve profile image
data, err = ApiTestData(GetProfileImage, "GET", "/profile/image", nil, nil,
APP_TOKENAPP, set.A.Token, nil)
assert.NoError(t, err)
// compare retrieved image
img, err = base64.StdEncoding.DecodeString(image)
assert.NoError(t, err)
assert.Zero(t, bytes.Compare(img, data))
}