1/*
2Copyright (c) 2015 VMware, Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package session
18
19import (
20	"context"
21	"net/url"
22	"testing"
23
24	"github.com/vmware/govmomi/test"
25	"github.com/vmware/govmomi/vim25"
26	"github.com/vmware/govmomi/vim25/soap"
27)
28
29func sessionClient(u *url.URL, t *testing.T) *Manager {
30	soapClient := soap.NewClient(u, true)
31	vimClient, err := vim25.NewClient(context.Background(), soapClient)
32	if err != nil {
33		t.Fatal(err)
34	}
35
36	return NewManager(vimClient)
37}
38
39func TestLogin(t *testing.T) {
40	u := test.URL()
41	if u == nil {
42		t.SkipNow()
43	}
44
45	session := sessionClient(u, t)
46	err := session.Login(context.Background(), u.User)
47	if err != nil {
48		t.Errorf("Expected no error, got %v", err)
49	}
50}
51
52func TestLogout(t *testing.T) {
53	u := test.URL()
54	if u == nil {
55		t.SkipNow()
56	}
57
58	session := sessionClient(u, t)
59	err := session.Login(context.Background(), u.User)
60	if err != nil {
61		t.Error("Login Error: ", err)
62	}
63
64	err = session.Logout(context.Background())
65	if err != nil {
66		t.Errorf("Expected nil, got %v", err)
67	}
68
69	err = session.Logout(context.Background())
70	if err == nil {
71		t.Errorf("Expected NotAuthenticated, got nil")
72	}
73}
74
75func TestSessionIsActive(t *testing.T) {
76	u := test.URL()
77	if u == nil {
78		t.SkipNow()
79	}
80
81	session := sessionClient(u, t)
82
83	// Skip test against ESXi -- SessionIsActive is not implemented
84	if session.client.ServiceContent.About.ApiType != "VirtualCenter" {
85		t.Skipf("Talking to %s instead of %s", session.client.ServiceContent.About.ApiType, "VirtualCenter")
86	}
87
88	err := session.Login(context.Background(), u.User)
89	if err != nil {
90		t.Error("Login Error: ", err)
91	}
92
93	active, err := session.SessionIsActive(context.Background())
94	if err != nil || !active {
95		t.Errorf("Expected %t, got %t", true, active)
96		t.Errorf("Expected nil, got %v", err)
97	}
98
99	session.Logout(context.Background())
100
101	active, err = session.SessionIsActive(context.Background())
102	if err == nil || active {
103		t.Errorf("Expected %t, got %t", false, active)
104		t.Errorf("Expected NotAuthenticated, got %v", err)
105	}
106}
107