1//
2// Copyright 2021, Pavel Kostohrys
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17package gitlab
18
19import (
20	"encoding/json"
21	"net/http"
22	"testing"
23)
24
25func TestGetAvatar(t *testing.T) {
26	mux, server, client := setup(t)
27	defer teardown(server)
28
29	const url = "https://www.gravatar.com/avatar/10e6bf7bcf22c2f00a3ef684b4ada178"
30
31	mux.HandleFunc("/api/v4/avatar",
32		func(w http.ResponseWriter, r *http.Request) {
33			testMethod(t, r, http.MethodGet)
34			w.WriteHeader(http.StatusAccepted)
35			avatar := Avatar{AvatarURL: url}
36			resp, _ := json.Marshal(avatar)
37			_, _ = w.Write(resp)
38		},
39	)
40
41	opt := &GetAvatarOptions{Email: String("sander@vanharmelen.nnl")}
42	avatar, resp, err := client.Avatar.GetAvatar(opt)
43	if err != nil {
44		t.Fatalf("Avatar.GetAvatar returned error: %v", err)
45	}
46
47	if resp.Status != "202 Accepted" {
48		t.Fatalf("Avatar.GetAvatar returned wrong status code: %v", resp.Status)
49	}
50
51	if url != avatar.AvatarURL {
52		t.Errorf("Avatar.GetAvatar wrong result %s, want %s", avatar.AvatarURL, url)
53	}
54}
55