I noticed that the login as doesn't completely work with groups. This is because it only clears and sets the current courses group information. The problem is, that if I have other course group info cached, when I go to those courses, I'm not in the right group. Worse still, if I revert back to my original login in a different course, the group from the course I was in gets restored.
I believe the code should be changes as in the attachment (look for '*** BUG' comments). That is, set and clear the entire group cache.