Skip to content

Commit

Permalink
add tests for domain/auth
Browse files Browse the repository at this point in the history
  • Loading branch information
ianic committed Feb 15, 2022
1 parent 062308e commit 0a09cb9
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
73 changes: 73 additions & 0 deletions domain/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package domain

import (
"strings"
"testing"
"time"

"github.com/mantil-io/mantil/kit/token"
"github.com/stretchr/testify/require"
)

func TestStoreReadUserClaims(t *testing.T) {
c := AccessTokenClaims{
Workspace: "workspace",
Project: "project",
Stage: "stage",
Runtime: "runtime",
Username: "username",
Role: Maintainer,
}
ctx := make(map[string]interface{})
StoreUserClaims(&c, ctx)
require.Len(t, ctx, 7)
require.Equal(t, c.Workspace, ctx["mantilWorkspace"])
require.Equal(t, c.Project, ctx["mantilProject"])
require.Equal(t, c.Stage, ctx["mantilStage"])
require.Equal(t, c.Runtime, ctx["mantilRuntime"])
require.Equal(t, c.Username, ctx["mantilUsername"])
require.Equal(t, c.Role, ctx["mantilRole"])
claimsMarshaled := "{\"w\":\"workspace\",\"p\":\"project\",\"s\":\"stage\",\"r\":\"runtime\",\"u\":\"username\",\"o\":1}"
require.Equal(t, ctx["mantilUserClaims"], claimsMarshaled)
}

func TestReadUserClaims(t *testing.T) {
c := AccessTokenClaims{
Workspace: "workspace",
Project: "project",
Stage: "stage",
Runtime: "runtime",
Username: "username",
Role: Maintainer,
}

publicKey, privateKey, err := token.KeyPair()
headers := make(map[string]string)

_, err = ReadAccessToken(headers)
require.Error(t, err)
require.Contains(t, err.Error(), "token not found")

token, err := token.JWT(privateKey, c, 7*24*time.Hour)
require.NoError(t, err)

headers[strings.ToLower(AccessTokenHeader)] = token
_, err = ReadAccessToken(headers)
require.Error(t, err)
require.Contains(t, err.Error(), "key not found")

// happy path
t.Setenv(EnvPublicKey, publicKey)
c2, err := ReadAccessToken(headers)
require.NoError(t, err)
require.Equal(t, &c, c2)

headers[strings.ToLower(AccessTokenHeader)] = token
c2, err = ReadAccessToken(headers)
require.NoError(t, err)
require.Equal(t, &c, c2)

headers[AccessTokenHeader] = "foo"
_, err = ReadAccessToken(headers)
require.Error(t, err)
}
3 changes: 3 additions & 0 deletions domain/testdata/config/state.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ stages:
- name: mister1
default: true
node: dev
keys:
public: ""
private: ""
endpoints:
rest: https://1b5nlkdaa4.execute-api.eu-central-1.amazonaws.com
ws: wss://4fm76gvdx4.execute-api.eu-central-1.amazonaws.com
Expand Down

0 comments on commit 0a09cb9

Please sign in to comment.