2021-03-01

Gitlab API: Problem with Unittesting Issues, Projects and Login

I have to write some unit tests for a college project which is based on the gitlab api. A few weeks ago we had to change our login function as our old one didn't work anymore and switched to https://www.gitlab.com/oauth/token. This thing works now, however it messed up all our tests and I can't get them to run anymore.

There are now differences between calling a method from the tests and calling it from the controller.

For example calling https://www.gitlab.com/api/v4/projects/{projectId} containes a JsonObject called "Permission" when the method is called from the controller however it doesn't have it when I call the exact same method with the exact same values from the test.

public async Task<int> GetAccessLevelOfUserForProject(string token, int projectId)
        {
            Console.WriteLine($"GitlabService::GetAccessLevelOfUserForProject Token: {token} projectId: {projectId}");

            var project = await rest.Get<GitlabProjects>($"https://www.gitlab.com/api/v4/projects/{projectId}",
                          new Dictionary<string, string> { { "Authorization", $"Bearer {value}" });
            int aLevel = 0;
            if (project.Permissions.Project_access != null)
                aLevel = project.Permissions.Project_access.Access_level;
            else if(project.Permissions.Group_access != null)
                aLevel = project.Permissions.Group_access.Access_level;

            return aLevel;
        }

When I want to login I always get an 401 unauthorized with the Content {"error":"invalid_client","error_description":"Client authentication failed due to unknown client, no client authentication included, or unsupported authentication method."} although it uses the same method as the other login which works and it also uses the same client authentication.

public async Task<string> Login(LoginDTO login)
        {
            Console.WriteLine($"GitlabService::Login username:{login.Username}");
            var client = new RestClient($"{baseUrl}/oauth/token");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            client.Authenticator = new HttpBasicAuthenticator({clientusername}, {clientpassword});
            request.AddHeader("Content-Type", $"application/x-www-form-urlencoded");
            request.AddParameter("grant_type", "password");
            request.AddParameter("username", login.Username);
            request.AddParameter("password", login.Password);
            IRestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);

            var data = JsonConvert.DeserializeObject<GitlabTokenData>(response.Content);
            return data.Access_token;
}

Replace {clientusername} and {clientpassword} with your client authentication.

There are even more cases where things like this happen and I don't understand why. Maybe someone who is more comfortable with this topic can help me out?



from Recent Questions - Stack Overflow https://ift.tt/3dU9QB4
https://ift.tt/eA8V8J

No comments:

Post a Comment