File: api_keys_test.go

package info (click to toggle)
golang-github-zorkian-go-datadog-api 2.30.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,612 kB
  • sloc: makefile: 28; sh: 13
file content (120 lines) | stat: -rw-r--r-- 3,448 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Datadog API for Go
 *
 * Please see the included LICENSE file for licensing information.
 *
 * Copyright 2019 by authors and contributors.
 */
package integration

import (
	"testing"

	"github.com/zorkian/go-datadog-api"
)

func TestAPIKeyCreateGetAndDelete(t *testing.T) {
	keyName := "client-test-key"
	expected, err := client.CreateAPIKey(keyName)
	if err != nil {
		t.Fatalf("Creating API key failed when it shouldn't. (%s)", err)
	}
	defer cleanUpAPIKey(t, *expected.Key)

	if *expected.Name != keyName {
		t.Errorf("Created key has wrong name. Got %s, want %s", *expected.Name, keyName)
	}

	// now try to fetch it freshly and compare it again
	actual, err := client.GetAPIKey(*expected.Key)
	if err != nil {
		t.Fatalf("Retrieving API key failed when it shouldn't. (%s)", err)
	}
	assertAPIKeyEquals(t, actual, expected)
}

func TestAPIKeyUpdateName(t *testing.T) {
	keyName := "client-test-key"
	newKeyName := "client-test-key-new"
	keyStruct, err := client.CreateAPIKey(keyName)
	if err != nil {
		t.Fatalf("Creating API key failed when it shouldn't. (%s)", err)
	}
	defer cleanUpAPIKey(t, *keyStruct.Key)

	*keyStruct.Name = newKeyName
	err = client.UpdateAPIKey(keyStruct)
	if err != nil {
		t.Fatalf("Updating API key failed when it shouldn't. (%s)", err)
	}

	if *keyStruct.Name != newKeyName {
		t.Errorf("API key name not updated. Got %s, want %s", *keyStruct.Name, newKeyName)
	}
}

func TestAPIKeyGetMultipleKeys(t *testing.T) {
	key1Name := "client-test-1"
	key2Name := "client-test-2"
	key1, err := client.CreateAPIKey(key1Name)
	if err != nil {
		t.Fatalf("Creating API key failed when it shouldn't. (%s)", err)
	}
	defer cleanUpAPIKey(t, *key1.Key)
	key2, err := client.CreateAPIKey(key2Name)
	if err != nil {
		t.Fatalf("Creating API key failed when it shouldn't. (%s)", err)
	}
	defer cleanUpAPIKey(t, *key2.Key)
	allKeys, err := client.GetAPIKeys()
	if err != nil {
		t.Fatalf("Getting all API keys failed when it shouldn't (%s)", err)
	}
	key1Found, key2Found := false, false
	for _, key := range allKeys {
		switch *key.Name {
		case key1Name:
			assertAPIKeyEquals(t, &key, key1)
			key1Found = true
		case key2Name:
			assertAPIKeyEquals(t, &key, key2)
			key2Found = true
		}
	}
	if key1Found == false {
		t.Errorf("Key 1 not found while getting multiple keys.")
	}
	if key2Found == false {
		t.Errorf("Key 2 not found while getting multiple keys.")
	}
}

func assertAPIKeyEquals(t *testing.T, actual, expected *datadog.APIKey) {
	if *actual.Created != *expected.Created {
		t.Errorf("APIKey created does not match: %s != %s", *actual.Created, *expected.Created)
	}
	if *actual.CreatedBy != *expected.CreatedBy {
		t.Errorf("APIKey created_by does not match: %s != %s", *actual.CreatedBy, *expected.CreatedBy)
	}
	if *actual.Key != *expected.Key {
		t.Errorf("APIKey key does not match: %s != %s", *actual.Key, *expected.Key)
	}
	if *actual.Name != *expected.Name {
		t.Errorf("APIKey name does not match: %s != %s", *actual.Name, *expected.Name)
	}
}

func cleanUpAPIKey(t *testing.T, key string) {
	if err := client.DeleteAPIKey(key); err != nil {
		t.Fatalf("Deleting api key failed when it shouldn't. Manual cleanup needed. (%s)", err)
	}

	deletedKey, err := client.GetAPIKey(key)
	if deletedKey != nil {
		t.Fatal("API key hasn't been deleted when it should have been. Manual cleanup needed.")
	}

	if err == nil {
		t.Fatal("Fetching deleted API key didn't lead to an error. Manual cleanup needed.")
	}
}