Browse Source

Clean up tests further.

Chris Rhodes 9 years ago
parent
commit
5f9326d165
2 changed files with 26 additions and 57 deletions
  1. 12 33
      discord_test.go
  2. 14 24
      restapi_test.go

+ 12 - 33
discord_test.go

@@ -21,6 +21,16 @@ var (
 	envAdmin    string = os.Getenv("DG_ADMIN")    // User ID of admin user to use for tests
 )
 
+func init() {
+	if envEmail == "" || envPassword == "" || envToken == "" {
+		return
+	}
+
+	if d, err := New(envEmail, envPassword, envToken); err == nil {
+		dg = d
+	}
+}
+
 //////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////// HELPER FUNCTIONS USED FOR TESTING
 
@@ -76,15 +86,10 @@ func TestNew(t *testing.T) {
 
 // TestInvalidToken tests the New() function with an invalid token
 func TestInvalidToken(t *testing.T) {
-	d, err := New("asjkldhflkjasdh")
+	_, err := New("asjkldhflkjasdh")
 	if err != nil {
 		t.Fatalf("New(InvalidToken) returned error: %+v", err)
 	}
-
-	if err = d.OpenAndListen(); err == nil {
-		t.Fatalf("New(InvalidToken), d.OpenAndListen did not fail.")
-	}
-
 }
 
 // TestInvalidUserPass tests the New() function with an invalid Email and Pass
@@ -132,20 +137,6 @@ func TestNewUserPass(t *testing.T) {
 	if d.Token == "" {
 		t.Fatal("New(user,pass), d.Token is empty, should be a valid Token.")
 	}
-
-	if err = d.OpenAndListen(); err != nil {
-		t.Fatalf("New(user,pass), d.OpenAndListen failed: %+v", err)
-	}
-
-	if !waitBoolEqual(10*time.Second, &d.DataReady, true) {
-		t.Fatal("New(user,pass), d.DataReady is false after 10 seconds.  Should be true.")
-	}
-
-	t.Log("Successfully connected to Discord via New(user,pass).")
-	dg = d
-	if envToken == "" {
-		envToken = dg.Token
-	}
 }
 
 // TestNewToken tests the New() function with a Token.  This should return
@@ -168,21 +159,9 @@ func TestNewToken(t *testing.T) {
 	if d.Token == "" {
 		t.Fatal("New(envToken), d.Token is empty, should be a valid Token.")
 	}
-
-	if err = d.OpenAndListen(); err != nil {
-		t.Fatalf("New(envToken), d.OpenAndListen failed: %+v", err)
-	}
-
-	if !waitBoolEqual(10*time.Second, &d.DataReady, true) {
-		t.Fatal("New(envToken), d.DataReady is false after 10 seconds.  Should be true.")
-	}
-
-	t.Log("Successfully connected to Discord via New(token).")
-	dg = d
-
 }
 
-func TestClose(t *testing.T) {
+func TestOpenClose(t *testing.T) {
 	if envToken == "" {
 		t.Skip("Skipping TestClose, DG_TOKEN not set")
 	}

+ 14 - 24
restapi_test.go

@@ -10,8 +10,8 @@ import (
 // TestLogout tests the Logout() function. This should not return an error.
 func TestLogout(t *testing.T) {
 
-	if dg == nil || dg.Token == "" {
-		t.Skip("Cannot test logout, dg.Token not set.")
+	if dg == nil {
+		t.Skip("Cannot TestLogout, dg not set.")
 	}
 
 	err := dg.Logout()
@@ -21,8 +21,8 @@ func TestLogout(t *testing.T) {
 }
 
 func TestUserAvatar(t *testing.T) {
-	if !isConnected() {
-		t.Skip("Skipped, Not connected to Discord.")
+	if dg == nil {
+		t.Skip("Cannot TestUserAvatar, dg not set.")
 	}
 
 	a, err := dg.UserAvatar("@me")
@@ -39,14 +39,8 @@ func TestUserAvatar(t *testing.T) {
 }
 
 func TestUserUpdate(t *testing.T) {
-
-	if envEmail == "" || envPassword == "" {
-		t.Skip("Skipping, DG_USERNAME or DG_PASSWORD not set")
-		return
-	}
-
-	if !isConnected() {
-		t.Skip("Skipped, Not connected to Discord.")
+	if dg == nil {
+		t.Skip("Cannot test logout, dg not set.")
 	}
 
 	u, err := dg.User("@me")
@@ -73,9 +67,8 @@ func TestUserUpdate(t *testing.T) {
 //func (s *Session) UserChannelCreate(recipientID string) (st *Channel, err error) {
 
 func TestUserChannelCreate(t *testing.T) {
-
-	if !isConnected() {
-		t.Skip("Skipped, Not connected to Discord.")
+	if dg == nil {
+		t.Skip("Cannot TestUserChannelCreate, dg not set.")
 	}
 
 	if envAdmin == "" {
@@ -91,9 +84,8 @@ func TestUserChannelCreate(t *testing.T) {
 }
 
 func TestUserChannels(t *testing.T) {
-
-	if !isConnected() {
-		t.Skip("Skipped, Not connected to Discord.")
+	if dg == nil {
+		t.Skip("Cannot TestUserChannels, dg not set.")
 	}
 
 	_, err := dg.UserChannels()
@@ -103,9 +95,8 @@ func TestUserChannels(t *testing.T) {
 }
 
 func TestUserGuilds(t *testing.T) {
-
-	if !isConnected() {
-		t.Skip("Skipped, Not connected to Discord.")
+	if dg == nil {
+		t.Skip("Cannot TestUserGuilds, dg not set.")
 	}
 
 	_, err := dg.UserGuilds()
@@ -115,9 +106,8 @@ func TestUserGuilds(t *testing.T) {
 }
 
 func TestUserSettings(t *testing.T) {
-
-	if !isConnected() {
-		t.Skip("Skipped, Not connected to Discord.")
+	if dg == nil {
+		t.Skip("Cannot TestUserSettings, dg not set.")
 	}
 
 	_, err := dg.UserSettings()