mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
validating create token in test
This commit is contained in:
parent
789c79cc25
commit
17602f169a
@ -355,6 +355,11 @@ paths:
|
|||||||
responses:
|
responses:
|
||||||
'200':
|
'200':
|
||||||
description: success
|
description: success
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
'401':
|
||||||
'401':
|
'401':
|
||||||
description: permission denied
|
description: permission denied
|
||||||
'500':
|
'500':
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
# Go API Server for databag
|
|
||||||
|
|
||||||
DataBag provides storage for decentralized identity based self-hosting apps. It is intended to support sharing of personal data and hosting group conversations.
|
|
||||||
|
|
||||||
## Overview
|
|
||||||
This server was generated by the [swagger-codegen]
|
|
||||||
(https://github.com/swagger-api/swagger-codegen) project.
|
|
||||||
By using the [OpenAPI-Spec](https://github.com/OAI/OpenAPI-Specification) from a remote server, you can easily generate a server stub.
|
|
||||||
-
|
|
||||||
|
|
||||||
To see how to make this your own, look here:
|
|
||||||
|
|
||||||
[README](https://github.com/swagger-api/swagger-codegen/blob/master/README.md)
|
|
||||||
|
|
||||||
- API version: 0.0.1
|
|
||||||
- Build date: 2022-01-13T17:14:57.205Z[GMT]
|
|
||||||
|
|
||||||
|
|
||||||
### Running the server
|
|
||||||
To run the server, follow these simple steps:
|
|
||||||
|
|
||||||
```
|
|
||||||
go run main.go
|
|
||||||
```
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
|||||||
package databag
|
package databag
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
@ -10,12 +10,12 @@ import (
|
|||||||
|
|
||||||
func TestAccount(t *testing.T) {
|
func TestAccount(t *testing.T) {
|
||||||
|
|
||||||
|
// acquire new token for creating accounts
|
||||||
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
|
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
|
||||||
r := httptest.NewRequest("POST", "/admin/accounts", nil)
|
r := httptest.NewRequest("POST", "/admin/accounts", nil)
|
||||||
r.Header.Add("Authorization","Basic " + auth)
|
r.Header.Add("Authorization","Basic " + auth)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
AddNodeAccount(w, r);
|
AddNodeAccount(w, r);
|
||||||
|
|
||||||
resp := w.Result();
|
resp := w.Result();
|
||||||
dec := json.NewDecoder(resp.Body);
|
dec := json.NewDecoder(resp.Body);
|
||||||
var token string;
|
var token string;
|
||||||
@ -24,5 +24,21 @@ func TestAccount(t *testing.T) {
|
|||||||
t.Errorf("failed to create account")
|
t.Errorf("failed to create account")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(token);
|
// validate account token
|
||||||
|
r = httptest.NewRequest("GET", "/account/token", nil)
|
||||||
|
r.Header.Add("Authorization","Bearer " + token)
|
||||||
|
w = httptest.NewRecorder()
|
||||||
|
GetAccountToken(w, r);
|
||||||
|
resp = w.Result();
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Errorf("invalid token value")
|
||||||
|
}
|
||||||
|
dec = json.NewDecoder(resp.Body);
|
||||||
|
var tokenType string;
|
||||||
|
dec.Decode(&tokenType);
|
||||||
|
if tokenType != "create" {
|
||||||
|
t.Errorf("invalid token type")
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("TestAccount: done");
|
||||||
}
|
}
|
||||||
|
@ -27,12 +27,15 @@ func AddNodeAccount(w http.ResponseWriter, r *http.Request) {
|
|||||||
token := store.AccountToken{TokenType: "create", Token: data };
|
token := store.AccountToken{TokenType: "create", Token: data };
|
||||||
if res := store.DB.Create(&token).Error; res != nil {
|
if res := store.DB.Create(&token).Error; res != nil {
|
||||||
log.Println("AddNodeAccount - failed to store token");
|
log.Println("AddNodeAccount - failed to store token");
|
||||||
|
w.WriteHeader(http.StatusInternalServerError);
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := json.Marshal(data);
|
body, err := json.Marshal(data);
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("GetNodeConfig - failed to marshal response");
|
log.Println("GetNodeConfig - failed to marshal response");
|
||||||
|
w.WriteHeader(http.StatusInternalServerError);
|
||||||
|
return
|
||||||
}
|
}
|
||||||
w.Write(body)
|
w.Write(body)
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
|
@ -32,3 +32,6 @@ func adminLogin(r *http.Request) bool {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func bearerAuth(r *http.Request) string {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
@ -63,11 +63,6 @@ func GetAccountStatus(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAccountToken(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAccountUsername(w http.ResponseWriter, r *http.Request) {
|
func GetAccountUsername(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
45
net/server/internal/getAccountToken_endpoint.go
Normal file
45
net/server/internal/getAccountToken_endpoint.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package databag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"encoding/json"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"databag/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetAccountToken(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
// extract token
|
||||||
|
auth := r.Header.Get("Authorization")
|
||||||
|
token := strings.TrimSpace(strings.TrimPrefix(auth, "Bearer"))
|
||||||
|
|
||||||
|
// lookup token
|
||||||
|
var accountToken store.AccountToken
|
||||||
|
err := store.DB.Where("token = ?", token).First(&accountToken).Error
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
log.Println("GetAccountToken - token not found");
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
} else {
|
||||||
|
log.Println("GetAccountToken - failed to retrieve token");
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// return token type
|
||||||
|
body, err := json.Marshal(accountToken.TokenType);
|
||||||
|
if err != nil {
|
||||||
|
log.Println("GetNodeConfig - failed to marshal response");
|
||||||
|
w.WriteHeader(http.StatusInternalServerError);
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Write(body);
|
||||||
|
w.Header().Set("Content-Type", "application/json charset=UTF-8")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user