Multiple playgrounds support (#215)
* Add Playground struct and basic support for creating it and retrieving it * Add missing functions in pwd mock * Get playground from request domain and validate it exists. If valid set it on the newly created session. * Move playground specific configurations to the playground struct and use it everytime we need that conf. * Don't allow to specify a duration bigger that the allowed in the playground
This commit is contained in:
committed by
GitHub
parent
3dee0d3f0b
commit
3f5b3882dd
@@ -30,12 +30,13 @@ func TestSessionPut(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Playgrounds: map[string]*types.Playground{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
@@ -59,12 +60,13 @@ func TestSessionPut(t *testing.T) {
|
||||
func TestSessionGet(t *testing.T) {
|
||||
expectedSession := &types.Session{Id: "aaabbbccc"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{expectedSession.Id: expectedSession},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Sessions: map[string]*types.Session{expectedSession.Id: expectedSession},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Playgrounds: map[string]*types.Playground{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
@@ -98,12 +100,13 @@ func TestSessionGetAll(t *testing.T) {
|
||||
s1 := &types.Session{Id: "aaabbbccc"}
|
||||
s2 := &types.Session{Id: "dddeeefff"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{s1.Id: s1, s2.Id: s2},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Sessions: map[string]*types.Session{s1.Id: s1, s2.Id: s2},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Playgrounds: map[string]*types.Playground{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
@@ -163,12 +166,13 @@ func TestSessionDelete(t *testing.T) {
|
||||
func TestInstanceGet(t *testing.T) {
|
||||
expectedInstance := &types.Instance{SessionId: "aaabbbccc", Name: "i1", IP: "10.0.0.1"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{expectedInstance.Name: expectedInstance},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{expectedInstance.Name: expectedInstance},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Playgrounds: map[string]*types.Playground{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{expectedInstance.SessionId: []string{expectedInstance.Name}},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
@@ -217,12 +221,13 @@ func TestInstancePut(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{i.Name: i},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{i.Name: i},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Playgrounds: map[string]*types.Playground{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{i.SessionId: []string{i.Name}},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
@@ -280,12 +285,13 @@ func TestInstanceFindBySessionId(t *testing.T) {
|
||||
i1 := &types.Instance{SessionId: "aaabbbccc", Name: "c1"}
|
||||
i2 := &types.Instance{SessionId: "aaabbbccc", Name: "c2"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{i1.Name: i1, i2.Name: i2},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{i1.Name: i1, i2.Name: i2},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Playgrounds: map[string]*types.Playground{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{i1.SessionId: []string{i1.Name, i2.Name}},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
@@ -316,12 +322,13 @@ func TestWindowsInstanceGetAll(t *testing.T) {
|
||||
i1 := &types.WindowsInstance{SessionId: "aaabbbccc", Id: "i1"}
|
||||
i2 := &types.WindowsInstance{SessionId: "aaabbbccc", Id: "i2"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{i1.Id: i1, i2.Id: i2},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{i1.Id: i1, i2.Id: i2},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Playgrounds: map[string]*types.Playground{},
|
||||
WindowsInstancesBySessionId: map[string][]string{i1.SessionId: []string{i1.Id, i2.Id}},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
@@ -371,12 +378,13 @@ func TestWindowsInstancePut(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{i.Id: i},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{i.Id: i},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Playgrounds: map[string]*types.Playground{},
|
||||
WindowsInstancesBySessionId: map[string][]string{i.SessionId: []string{i.Id}},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
@@ -433,12 +441,13 @@ func TestWindowsInstanceDelete(t *testing.T) {
|
||||
func TestClientGet(t *testing.T) {
|
||||
c := &types.Client{SessionId: "aaabbbccc", Id: "c1"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{c.Id: c},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{c.Id: c},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Playgrounds: map[string]*types.Playground{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{c.SessionId: []string{c.Id}},
|
||||
@@ -487,12 +496,13 @@ func TestClientPut(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{c.Id: c},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{c.Id: c},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Playgrounds: map[string]*types.Playground{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{c.SessionId: []string{c.Id}},
|
||||
@@ -550,12 +560,13 @@ func TestClientFindBySessionId(t *testing.T) {
|
||||
c1 := &types.Client{SessionId: "aaabbbccc", Id: "c1"}
|
||||
c2 := &types.Client{SessionId: "aaabbbccc", Id: "c2"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{c1.Id: c1, c2.Id: c2},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{c1.Id: c1, c2.Id: c2},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Playgrounds: map[string]*types.Playground{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{c1.SessionId: []string{c1.Id, c2.Id}},
|
||||
@@ -581,3 +592,120 @@ func TestClientFindBySessionId(t *testing.T) {
|
||||
assert.Subset(t, clients, []*types.Client{c1, c2})
|
||||
assert.Len(t, clients, 2)
|
||||
}
|
||||
|
||||
func TestPlaygroundGet(t *testing.T) {
|
||||
p := &types.Playground{Id: "aaabbbccc"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Playgrounds: map[string]*types.Playground{p.Id: p},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
UsersByProvider: map[string]string{},
|
||||
}
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
encoder := json.NewEncoder(tmpfile)
|
||||
err = encoder.Encode(&expectedDB)
|
||||
assert.Nil(t, err)
|
||||
tmpfile.Close()
|
||||
defer os.Remove(tmpfile.Name())
|
||||
|
||||
storage, err := NewFileStorage(tmpfile.Name())
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
found, err := storage.PlaygroundGet("aaabbbccc")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, p, found)
|
||||
}
|
||||
|
||||
func TestPlaygroundPut(t *testing.T) {
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
tmpfile.Close()
|
||||
os.Remove(tmpfile.Name())
|
||||
defer os.Remove(tmpfile.Name())
|
||||
|
||||
storage, err := NewFileStorage(tmpfile.Name())
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
p := &types.Playground{Id: "aaabbbccc"}
|
||||
|
||||
err = storage.PlaygroundPut(p)
|
||||
assert.Nil(t, err)
|
||||
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Playgrounds: map[string]*types.Playground{p.Id: p},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
UsersByProvider: map[string]string{},
|
||||
}
|
||||
var loadedDB *DB
|
||||
|
||||
file, err := os.Open(tmpfile.Name())
|
||||
|
||||
assert.Nil(t, err)
|
||||
defer file.Close()
|
||||
|
||||
decoder := json.NewDecoder(file)
|
||||
err = decoder.Decode(&loadedDB)
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.EqualValues(t, expectedDB, loadedDB)
|
||||
}
|
||||
|
||||
func TestPlaygroundGetAll(t *testing.T) {
|
||||
p1 := &types.Playground{Id: "aaabbbccc"}
|
||||
p2 := &types.Playground{Id: "dddeeefff"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
Playgrounds: map[string]*types.Playground{p1.Id: p1, p2.Id: p2},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
UsersByProvider: map[string]string{},
|
||||
}
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
encoder := json.NewEncoder(tmpfile)
|
||||
err = encoder.Encode(&expectedDB)
|
||||
assert.Nil(t, err)
|
||||
tmpfile.Close()
|
||||
defer os.Remove(tmpfile.Name())
|
||||
|
||||
storage, err := NewFileStorage(tmpfile.Name())
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
found, err := storage.PlaygroundGetAll()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []*types.Playground{p1, p2}, found)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user