Create environmental var package

This commit is contained in:
Daniel Krol 2022-07-23 18:13:56 -04:00
parent 36659ef720
commit ade526f4f9
10 changed files with 45 additions and 21 deletions

16
env/env.go vendored Normal file
View file

@ -0,0 +1,16 @@
package env
import (
"os"
)
// For test stubs
type EnvInterface interface {
Getenv(key string) string
}
type Env struct{}
func (e *Env) Getenv(key string) string {
return os.Getenv(key)
}

View file

@ -4,6 +4,7 @@ import (
"log"
"lbryio/lbry-id/auth"
"lbryio/lbry-id/env"
"lbryio/lbry-id/server"
"lbryio/lbry-id/store"
)
@ -23,6 +24,6 @@ func storeInit() (s store.Store) {
func main() {
store := storeInit()
srv := server.Init(&auth.Auth{}, &store)
srv := server.Init(&auth.Auth{}, &store, &env.Env{})
srv.Serve()
}

View file

@ -13,9 +13,8 @@ import (
)
func TestServerRegisterSuccess(t *testing.T) {
testAuth := TestAuth{}
testStore := TestStore{}
s := Server{&testAuth, &testStore}
testStore := &TestStore{}
s := Server{&TestAuth{}, testStore, &TestEnv{}}
requestBody := []byte(`{"email": "abc@example.com", "password": "123", "clientSaltSeed": "abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234" }`)
@ -76,9 +75,7 @@ func TestServerRegisterErrors(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// Set this up to fail according to specification
testAuth := TestAuth{}
testStore := TestStore{Errors: tc.storeErrors}
server := Server{&testAuth, &testStore}
server := Server{&TestAuth{}, &TestStore{Errors: tc.storeErrors}, &TestEnv{}}
// Make request
requestBody := fmt.Sprintf(`{"email": "%s", "password": "123", "clientSaltSeed": "abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234"}`, tc.email)

View file

@ -17,7 +17,7 @@ import (
func TestServerAuthHandlerSuccess(t *testing.T) {
testAuth := TestAuth{TestNewTokenString: auth.TokenString("seekrit")}
testStore := TestStore{}
s := Server{&testAuth, &testStore}
s := Server{&testAuth, &testStore, &TestEnv{}}
requestBody := []byte(`{"deviceId": "dev-1", "email": "abc@example.com", "password": "123"}`)
@ -95,7 +95,7 @@ func TestServerAuthHandlerErrors(t *testing.T) {
if tc.authFailGenToken { // TODO - TestAuth{Errors:authErrors}
testAuth.FailGenToken = true
}
server := Server{&testAuth, &testStore}
server := Server{&testAuth, &testStore, &TestEnv{}}
// Make request
// So long as the JSON is well-formed, the content doesn't matter here since the password check will be stubbed out

View file

@ -66,7 +66,7 @@ func TestServerGetClientSalt(t *testing.T) {
Errors: tc.storeErrors,
}
s := Server{&testAuth, &testStore}
s := Server{&testAuth, &testStore, &TestEnv{}}
req := httptest.NewRequest(http.MethodGet, PathClientSaltSeed, nil)
q := req.URL.Query()

View file

@ -93,7 +93,7 @@ func TestIntegrationWalletUpdates(t *testing.T) {
st, tmpFile := storeTestInit(t)
defer storeTestCleanup(tmpFile)
s := Init(&auth.Auth{}, &st)
s := Server{&auth.Auth{}, &st, &TestEnv{}}
////////////////////
t.Log("Request: Register email address - any device")
@ -259,7 +259,7 @@ func TestIntegrationChangePassword(t *testing.T) {
st, tmpFile := storeTestInit(t)
defer storeTestCleanup(tmpFile)
s := Init(&auth.Auth{}, &st)
s := Server{&auth.Auth{}, &st, &TestEnv{}}
////////////////////
t.Log("Request: Register email address")

View file

@ -143,9 +143,8 @@ func TestServerChangePassword(t *testing.T) {
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
testAuth := TestAuth{}
testStore := TestStore{Errors: tc.storeErrors}
s := Server{&testAuth, &testStore}
s := Server{&TestAuth{}, &testStore, &TestEnv{}}
// Whether we passed in wallet fields (these test cases should be passing
// in all of them or none of them, so we only test EncryptedWallet). This

View file

@ -11,6 +11,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"lbryio/lbry-id/auth"
"lbryio/lbry-id/env"
"lbryio/lbry-id/store"
)
@ -33,16 +34,17 @@ const PathWrongApiVersion = "/api/"
type Server struct {
auth auth.AuthInterface
store store.StoreInterface
env env.EnvInterface
}
// TODO If I capitalize the `auth` `store` and `env` fields of Store{} I can
// create Store{} structs directly from main.go.
func Init(
auth auth.AuthInterface,
store store.StoreInterface,
env env.EnvInterface,
) *Server {
return &Server{
auth: auth,
store: store,
}
return &Server{auth, store, env}
}
type ErrorResponse struct {

View file

@ -17,6 +17,14 @@ import (
// Implementing interfaces for stubbed out packages
type TestEnv struct {
env map[string]string
}
func (e *TestEnv) Getenv(key string) string {
return e.env[key]
}
type TestAuth struct {
TestNewTokenString auth.TokenString
FailGenToken bool
@ -264,7 +272,7 @@ func TestServerHelperCheckAuth(t *testing.T) {
Errors: tc.storeErrors,
TestAuthToken: auth.AuthToken{Token: auth.TokenString("seekrit"), Scope: tc.userScope},
}
s := Server{&TestAuth{}, &testStore}
s := Server{&TestAuth{}, &testStore, &TestEnv{}}
w := httptest.NewRecorder()
authToken := s.checkAuth(w, testStore.TestAuthToken.Token, tc.requiredScope)

View file

@ -76,7 +76,8 @@ func TestServerGetWallet(t *testing.T) {
Errors: tc.storeErrors,
}
s := Server{&testAuth, &testStore}
testEnv := TestEnv{}
s := Server{&testAuth, &testStore, &testEnv}
req := httptest.NewRequest(http.MethodGet, PathWallet, nil)
q := req.URL.Query()
@ -234,7 +235,7 @@ func TestServerPostWallet(t *testing.T) {
Errors: tc.storeErrors,
}
s := Server{&testAuth, &testStore}
s := Server{&testAuth, &testStore, &TestEnv{}}
requestBody := []byte(
fmt.Sprintf(`{